Cast Future<Isar> to Isar

74 views Asked by At

I have this class:

class IsarHelperSiniestro {
  late Isar isarInstance;

  IsarHelperSiniestro() {
    isarInstance = IsarService().db as Isar;
  }

  static IsarHelperSiniestro instance = IsarHelperSiniestro();
}

And this class:

class IsarService{
  late Future<Isar> db;

  IsarService(){
    db = openDb();
  }

  Future<Isar> openDb() async {
    final dir = await getApplicationDocumentsDirectory();

    if(Isar.instanceNames.isEmpty){
      return await Isar.open([UsuarioSchema, SiniestroSchema, ActaSchema], directory: dir.path, inspector: true);
    }

    return Future.value(Isar.getInstance());
  }
}

But it is sending me the error shown in the screenshot:

enter image description here

My question is how do I make Future be passed to only Isar and pass it to the late Isar variable isarInstance.

Greetings!

1

There are 1 answers

1
Md. Yeasin Sheikh On BEST ANSWER

You can use .then on Future

 IsarHelperSiniestro() {
    IsarService().db.then((value) => isarInstance = value);
  }

I prefer creating Future instance for this case,

  late Future<Isar> _db;

And when I need to use it await _db.