Skip to content

How to access DB from background isolate in flutter? #3249

Answered by simolus3
azeem-io asked this question in Q&A
Discussion options

You must be logged in to vote

So generally, the best approach is to use drift_flutter for that, similar to what is being done here: #3229

So as long as the two isolates share a IsolateNameServer instance (I think they do since they're running in the same process), you can use the driftDatabase function from package:drift_flutter with the shareAcrossIsolates: true option) like this:

@DriftDatabase(...)
class AppDatabase extends _$AppDatabase {
  AppDatabase() : super(driftDatabase(name: 'app.db', native: DriftNativeOptions(shareAcrossIsolates: true)));

Then, regardless of the isolate you open your database in, they'll all talk to the same database instance. A downside is that this is currently broken for isolates in di…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by azeem-io
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants