Home > Back-end >  I/flutter ( 9532): Binding has not yet been initialized, when create database
I/flutter ( 9532): Binding has not yet been initialized, when create database

Time:06-07

I'm building an app with sqflite database, when I run my app i got this error:

I/flutter ( 9532): eeeeeeeee Binding has not yet been initialized. I/flutter ( 9532): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized. I/flutter ( 9532): Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding. I/flutter ( 9532): In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding. I/flutter ( 9532): If ServicesBinding is a custom binding mixin, there must also be a custom binding class, like WidgetsFlutterBinding, but that mixes in the selected binding, and that is the class that must be constructed before using the "instance" getter. nnnnnnnnnnnn

here is my main()

void main() async {
  WidgetsFlutterBinding.ensureInitialized;
  await DBHelper().initDb();
  runApp(const MyApp());
}

and here is the initDB()

  initDb() async {
    try {
      String databasePath = await getDatabasesPath(); //The break point stops here and show me the error above.

      String path = join(databasePath, 'task.db');

      Database ourdb = await openDatabase(path,
          onCreate: _onCreate, version: _version, onUpgrade: _onUpgrade);

      return ourdb;
    } catch (e) {
      print('eeeeeeeee $e nnnnnnnnnnnn');
    }
  }

So what is goes wrong?

CodePudding user response:

WidgetsFlutterBinding.ensureInitialized is just a statement - you should execute the function.

Replace WidgetsFlutterBinding.ensureInitialized with WidgetsFlutterBinding.ensureInitialized().

  • Related