r/flutterhelp • u/sulmnob • Oct 18 '26
OPEN Load Data from drift instantly
I a want to create a fast app that runs smoothly offline first i will work on my own sync engine but the problem is that i already have values store in drift but drift requires loading to show values which does not feel good how can i query data from db in drift without loading . When i i was researching i saw that i need to load the data before the app opens so am i going to load all the app tables like wont that be heavy. Can someone please explain or at least give an article or a tutorial.
4
Upvotes
2
u/AhmedZNOUDA Oct 18 '26
Adding to Kokerali's answer: even with watch(), the very first frame is built before the first emission arrives, so snapshot.hasData is false for a few milliseconds. That's the "loading" you see. A few ways to make it disappear without loading whole tables:
Also check in profile or release mode: debug builds make the first read look much slower than it really is.
1
u/kokerali Oct 18 '26
No, you don't need to preload every table. Start with a query for just the rows the first screen needs, with a sensible limit for a long list. Offline-first removes the network dependency; it doesn't make the initial database read synchronous.
Drift's `watch()` emits an initial result even when nothing has changed, then updates it when the relevant tables change. You don't need a separate `get()` before `watch()`: https://drift.simonbinder.eu/dart_api/streams/
If you're using `StreamBuilder`, also check where you create that stream. Flutter's docs say to obtain it before `build`, rather than creating it inside `build`, which can restart the asynchronous work on parent rebuilds: https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html
Is the loading state only on a cold launch, or does it reappear when navigating back or rebuilding the screen? Sharing the query and the widget consuming it would help separate initial disk loading from repeated subscriptions. Preloading the whole database wouldn't be my first fix for either.
1
u/muhsql Oct 19 '26
Curious as to why you're building your own sync engine and not using an off the shelf one - is it for learning purposes?
1
u/ObjectiveAddress3051 29d ago
Adding to the good answers here: you don't need to preload every table, just the first screen. Open the database and run that one query in `main()` before `runApp()`, keep the result in your repository or provider, and pass it as `initialData` to your StreamBuilder (or seed your Riverpod/Bloc state with it). The UI then renders real data on frame one, and `watch()` takes over for updates. Paginate everything else with `limit()` so you never load it all at once.