Reputation: 819
I am building an application using AWS AppSync which fetches the Posts from the AWS Appsync and is rendered via the FutureBuilder Widget > ListView.Builder Is there a way I can grab it as a stream and use a stream view builder to render the same as there is no official documentation provided by AWS Amplify regarding this?
Upvotes: 0
Views: 667
Reputation: 21
According to the documentation, it is possible to have a SusbcriptionEvent of the object and then listen to the stream.
Stream<SubscriptionEvent<Post>> stream =
Amplify.DataStore.observe(Post.classType);
stream.listen((event) {
print('Received event of type ' + event.eventType.toString());
print('Received post ' + event.item.toString());
});
An alternative is using a StreamBuilder, you could create a function that returns the stream
Stream postStream() {
Stream<SubscriptionEvent<Post>> stream =
Amplify.DataStore.observe(Post.classType);
return stream;
}
And then use a StreamBuilders to listen to the stream being returned by the function.
StreamBuilder<SubscriptionEvent<Post>>(
stream: postStream(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text('No data');
}
if (snapshot.hasError ||
snapshot.connectionState == ConnectionState.waiting) {
return Text('Loading...');
}
print("snapshot ${snapshot.data.item.title}");
print("snapshot ${snapshot.data.item.rating.toString()}");
return Column(
children: [
Text(snapshot.data.item.title),
Text(snapshot.data.item.rating.toString()),
],
);
}),
Upvotes: 1