Guidance on using PowerSync with popular Flutter state management libraries.
Future
and Stream
classes from dart:async
. Given how widely used these are
in the Dart ecosystem, PowerSync works well with all popular approaches for state management, such as:
package:provider
: Create your database as a Provider
and expose watched queries to child widgets with StreamProvider
!
The provider for databases should close()
the database in dispose
.package:riverpod
: We mention relevant snippets below.package:get_it
: PowerSync databases can be registered with registerSingletonAsync
. Again, make sure
to close()
the database in the dispose
callback.bloc
package: You can easily listen to watched queries in Cubits (although, if you find your
Blocs and Cubits becoming trivial wrappers around database streams, consider just watch()
ing database queries in widgets directly.
That doesn’t make your app less testable!).
To simplify state management, avoid the use of hydrated blocs and cubits for state that depends on database queries. With PowerSync,
regular data is already available locally and doesn’t need a second local cache.auto_route
.connect
and
disconnect
calls there, for instance by listening to the authentication state:
StreamProvider
reading the database:
waitForFirstSync
before, you can keep doing that: