Building Local-First Flutter Apps with Riverpod, Drift, and PowerSync

34 pointsposted 5 days ago
by kobieps

21 Comments

account-5

9 hours ago

I replied to this 3 days ago even though it looks like hours ago now. Either way I've another question, that's similar.

If this is a local first app, why not use the sqlite database itself for state management? Powersync themselves talk about it here:

https://www.powersync.com/blog/how-local-first-simplifies-fl...

Flutter state management always seemed clunky and complicated to me. With the caveat I'm developing small projects in it that tend not to need complicated state management functionality.

doawoo

15 hours ago

As a newer user of Flutter I found Riverpod to be extremely heavy and have a lot more mental overhead than using stateless widgets with Hooks.

Any particular reason you personally prefer Riverpod?

dinko7

12 hours ago

Hi, author of the article here.

Any state management approach requires you to adapt your way of thinking, whether that be BLoC, Riverpod, Redux or anything you want to use.

Rivepod gained popularity because it's really simple to pick up: create a Notifier, create a Provider for it, and observe, while some other approaches require additional boilerplate, setup, and understanding.

Your approach would work if you are only observing that state from a single widget, which might not always be the case. Additionally, assuming useState is using setState under the hood means it will rebuild the whole widget on change, while with Riverpod, you have the flexibility to wrap any part of a complex widget into a Consumer or listen to only part of the exposed state on the Notifier with .select().

To put it simply: - Notifiers are used for app state - Hooks are used for ephemeral state (local widget state)

Hope this clears it bit for you.

doawoo

12 hours ago

Great summary, it does indeed! Thanks for taking the time to reply

wiradikusuma

4 hours ago

Check MobX, that's what I ended up using.

zerr

13 hours ago

I wonder why Flutter didn't gain traction in US. It seems to be more or less popular in poor countries and even less in Europe. But in US it seems to be quite a no name. Why US is so JavaScript-centric?

taormina

9 hours ago

Uh what? It’s extremely popular in the US? You wanna source that claim?

vin047

11 hours ago

There are a lot more JS and Native developers compared to Flutter/Dart developers in the West. Plus fear-mongering around Google dropping development of Flutter.

kobieps

6 hours ago

Anecdotaly I've found this to be true. The cause is probably multifaceted. Some things I noticed:

- npm is legit slower in these countries

- Flutter's easy and stable toolchain and great cross-platform support counts a lot more in countries with less specialization

- lower on-disk footprint because no node_modules black hole, actually matters in countries without infinity disk size

- smaller app bundles mean less mobile data

Just some anecdotal observations, there are probably other factors too like inertia

zerr

19 minutes ago

In a similar trend, in many big American companies you can often find that in US they list front-end, full-stack and other similar js positions while e.g. C++ positions are "offshored" to the teams e.g. in India.

dleeftink

11 hours ago

> poor countries

Ah yes, those fluttering countries and their fluttery ways

hosh

10 hours ago

Let's be clear. This post describes an architecture that is offline-first, not local-first.

One of the main goals of local-first is so that the user of a local-first application owns their own data. (See Martin Kleppmann's paper on this).

As such, local-first applications don't necessarily have a concept of a central server. `git` is local-first, though most teams synchronize to a hub such as Github or Gitlab. This is a design principle to get away from having to sync to the cloud, making it more difficult to monetize as a SAAS. There seems to be a growing trend of people promoting offline-first applications as local-first, but structuring it to still lock people's data into their SAAS. (If you want to lock them in, then say so -- call it offline-first).

A true local-first mobile app would allow me to collaborate with someone in the same room using Bluetooth, even out somewhere where I don't have wifi, cell service or Starlink

See:

- https://martin.kleppmann.com/papers/local-first.pdf

- https://www.inkandswitch.com/essay/local-first/ (Same, but in html)

kobieps

6 hours ago

> A true local-first mobile app would allow me to collaborate with someone in the same room using Bluetooth, even out somewhere where I don't have wifi, cell service or Starlink

Are there any popular cross platform apps that actually do this? Genuine question, I don't know of any.

I won't speak on the author's behalf, but I think he was using the term loosely here to refer to an app that hydrates and mutates state against disk and asynchronously syncs with other users (via a sync service) in the background. Also, his post uses an architecture that connects to the devloper's own backend database (pg, mongo, etc) and not a proprietary backend-as-a-service. I don't see data lock-in here.

But yes, that is a trend. Even the conference has many talks that don't stick to the original 7 ideals. I think "sync" or "sync engine" is a more useful general purpose term that isn't bogged down by specifics.

sgt

14 hours ago

Would this work with Flutter Web as well?

kobieps

13 hours ago

Yes

sgt

12 hours ago

Flutter Web used to be pretty slow but I note that it has improved substantially in the last 2 years.

account-5

4 days ago

Why not just use sqlite instead of drift?

kobieps

3 days ago

Probably easier to ask an LLM, but here goes: drift gives you type-safe queries which lets you catch any errors at compile time instead of runtime (which is the case with sqlite). There are other benefits but that's probably the main one.

taormina

15 hours ago

It’s still SQLite. Drift as an ORM they are using on top of SQLite.