首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用rootBundle.load()阻塞UI加载大型数据库

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

Stack Overflow用户
提问于 2019-04-16 19:49:27
回答 1查看 104关注 0票数 0

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

代码:

代码语言:javascript
复制
  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

EN

回答 1

Stack Overflow用户

发布于 2019-04-16 20:37:43

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55707483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档