Flutter access database in background fetch on Android Flutter access database in background fetch on Android dart dart

Flutter access database in background fetch on Android


You can try looking at sqflite troubleshooting section on their Github page, especially the one related to your error. Let me paste the bits from that page here.

This error is typically a build/setup error after adding the dependency.

  • Try all the steps defined at the top of the documents
  • Make sure you stop the current running application if any
  • Force a flutter packages get
  • Try to clean your build folder flutter clean
  • On iOS, you can try to force a pod install / pod update
  • Search for other bugs in flutter like this, other people face the same issue with other plugins so it is likely not sqflite related

Advanced checks:

Check the GeneratedPluginRegistrant file that flutter run should have generated in your project contains a line registering the plugin.

Android:

SqflitePlugin.registerWith(registry.registrarFor("com.tekartik.sqflite.SqflitePlugin"));

iOS:

[SqflitePlugin registerWithRegistrar:[registry registrarForPlugin:@"SqflitePlugin"]];
  • Check MainActivity.java (Android) contains a call to GeneratedPluginRegistrant asking it to register itself. This call should be made from the app launch method (onCreate).
public class MainActivity extends FlutterActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        GeneratedPluginRegistrant.registerWith(this);    }}
  • Check AppDelegate.m (iOS) contains a call to GeneratedPluginRegistrant asking it to register itself. This call should be made from the app launch method (application:didFinishLaunchingWithOptions:).
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  [GeneratedPluginRegistrant registerWithRegistry:self];  return [super application:application didFinishLaunchingWithOptions:launchOptions];}

Before raising this issue, try adding another well established plugin (the simplest being path_provider or shared_preferences) to see if you get the error here as well.


There has been some experiments about using sqflite from a background isolate and I'm not sure about the plugin support here. Anyway the transaction mechanism is not safe across isolate in the same process so I advise using sqflite from the main isolate (it already uses its own thread).


If your case is the same as mine, I have a custom plugin with Kotlin, so I didn't import GeneratedPluginRegistrant.registerWith(this) at the MainActivity. Instead, I implemented it at the start of the configureFlutterEngine() function and it worked perfectly.

import io.flutter.plugins.GeneratedPluginRegistrantclass MainActivity : FlutterActivity() {    private val CHANNEL = "getEpubs"    var _eventSink: EventChannel.EventSink? = null    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {        GeneratedPluginRegistrant.registerWith(flutterEngine)}

This registers the generated sqflite plugin access to the database on the device.

(Sorry for the Kotlin code, but you can change it to Java.)

Don't forget to import:

io.flutter.plugins.GeneratedPluginRegistrant