Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Observe in real time

Amplify Flutter v1 is now in Maintenance Mode until April 30th, 2025. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Flutter to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Observe model mutations in real-time

You can subscribe to changes on your Models. This reacts dynamically to updates of data to the underlying Storage Engine, which could be the result of GraphQL Subscriptions as well as Queries or Mutations that run against the backing AppSync API if you are synchronizing with the cloud.

StreamSubscription<SubscriptionEvent<Post>>? stream;
void listenChanges() {
stream = Amplify.DataStore.observe(Post.classType).listen(
(event) {
print('Received event of type ' + event.eventType.toString());
print('Received post ' + event.item.toString());
},
);
}
void stopListeningChanges() {
stream?.cancel();
}

Observe query results in real-time

observeQuery(...) returns an initial data set, similar to query(...), and also automatically subscribes to subsequent changes to the query.

The first snapshot returned from observeQuery will contain the same results as calling query directly on your Model - a collection of all the locally-available records. Subsequent snapshots will be emitted as updates to the dataset are received in the local store.

While data is syncing from the cloud, snapshots will contain all of the items synced so far and an isSynced status of false. When the sync process is complete, a snapshot will be emitted with all the records in the local store and an isSynced status of true.

In addition to typical real-time use cases, observeQuery can be used on app launch to show your customers an initial data set from the local store while new data is being synced from cloud.

observeQuery also accepts the same predicates and sorting options as query.

void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
StreamSubscription<QuerySnapshot<Post>>? _stream;
// Initialize a list for storing posts
List<Post> _posts = [];
// Initialize a boolean indicating if the sync process has completed
bool _isSynced = false;
// Initialize the libraries
...
void observeQuery() {
_stream = Amplify.DataStore.observeQuery(
Post.classType,
where: Post.TITLE.beginsWith('post') & (Post.RATING > 10),
sortBy: [Post.RATING.ascending()],
).listen((QuerySnapshot<Post> snapshot) {
setState(() {
_posts = snapshot.items;
_isSynced = snapshot.isSynced;
});
});
}
// Build function and UI elements
...
void dispose() {
_stream?.cancel();
super.dispose();
}
}