0

I'm trying to convert Amplify.DataStore.observeQuery into a stream in repository class.
The expected return type is Stream<List<MyObject>>.

1 Answer 1

0

In the repository class you can write:

Stream<List<MyObject>> watchMessages() async* {
yield* Amplify.DataStore.observeQuery(MyObject.classType,
            where: MyObject.ID.eq(myId))
        .map((snapshot) => snapshot.items));
  }
}

or if you have to convert objects to domain then:

Stream<List<MyObject>> watchMessages() async* {
yield* Amplify.DataStore.observeQuery(MyObject.classType,
            where: MyObject.ID.eq(myId))
        .map((snapshot) => snapshot.items.map((e) => e.toDomain()).toList());
  }
}

Then in the bloc you can emit states on every stream input:

MyBloc(this._repository) : super(MyState.initial()) {
    on<_WatchMessages>((_onWatchMessages));
  }

void _onWatchMessages(_WatchMessages event, Emitter<MyState> emit) async {
    await emit.forEach(_repository.watchMessages(),
        onData: ((List<MyObject> messages) {
      return state.copyWith(messages: List.from(messages));
    }));
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.