使用rootBundle.load()阻止UI的大型数据库加载

问题描述 投票:0回答:1

我在资产文件夹中有一个大型数据库(大约130mb)。但是当我使用rootBundle.load()时,UI会阻塞3-4秒。有解决方案吗

码:

  Future<Database> initDb() async {
    var databasesPath = await getDatabasesPath();
    var path = join(databasesPath, "hadith_db.db");

    bool fileExists = File(path).existsSync();
    if (!fileExists) {
      // Should happen only the first time you launch your application
      print("Creating new copy from asset");

      // Copy from asset
      print("starting root bundle");
      ByteData data = await rootBundle.load(join("assets", "hadith_db.db"));
      print("starting buffer");
      List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
      print("finished buffer");
      await File(path).writeAsBytes(bytes);
    }

    // open the database
    Database db = await openDatabase(path, readOnly: true);

    return db;
  }

注意:为了显示这些数据我正在使用FutureBuilder

dart flutter
1个回答
0
投票

你可以使用dart isolates。 Dart隔离用于并发编程,并创建类似于线程但不共享内存的工作者,它们仅通过消息传递工作。有关更多信息,请参阅:https://api.dartlang.org/stable/2.2.0/dart-isolate/dart-isolate-library.html

© www.soinside.com 2019 - 2024. All rights reserved.