Home > Mobile >  Flutter: Database being accessed too quickly
Flutter: Database being accessed too quickly

Time:08-24

This is a pretty basic question but I am stuck and would appreciate any assistance.

I am using a Hive database and have a condition to check if the database is empty. If it is empty, I have a function to copy another database to it. But the database is being called before the copy is complete resulting in a RangeError: 0. How do I make it wait until the copy is complete before accessing it?

Calling Code

    Box db = Hive.box<CardFields>("starter_worker");
    dbcheck(db);
    int currentcardindex = randomcard(db); // Function to generate random index based on db length
    CardFields currentCard = db.getAt(currentcardindex);

dbcheck()

dbcheck(Box db) async {
    if (db.isEmpty) {
      await refilldb("starter");
    }
  }

refilldb()

refilldb(String db) async {
  Directory devicedir = await getApplicationDocumentsDirectory();
  var workerpath = join(devicedir.path   "/db/"   db   "_worker.hive");
  var statpath = join(devicedir.path   "/db/"   db   ".hive");
  if (File(workerpath).existsSync() == false) {
    new File(workerpath).create(recursive: true);
  }
  await File(statpath).copy(workerpath);
}

CodePudding user response:

You should try to await your dbcheck function.

await dbcheck(db);

CodePudding user response:

"too quickly" can't be something a developer faces.. you must await for async methods and calls..

in the build method you have to use a FutureBuilder in order to await for the execution of a function. inside the builder you can check the state of your data so you can display a loading widget before your data is ready

  • Related