Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 预推SQLite数据库,在第一次启动时总是空的-颤振_Flutter_Dart_Sqflite - Fatal编程技术网

Flutter 预推SQLite数据库,在第一次启动时总是空的-颤振

Flutter 预推SQLite数据库,在第一次启动时总是空的-颤振,flutter,dart,sqflite,Flutter,Dart,Sqflite,每次我的应用程序第一次启动或数据库被删除时,复制应用程序后会重新启动(如果数据库被删除,但如果是第一次启动,则不会重新启动),并且此错误总是发生: RangeError(索引):无效值:有效值范围为空:0 之所以会发生这种情况,是因为小部件树上的小部件需要一个列表,但据推测数据库上没有值 但是如果我进行热重启,数据库现在就有了值并且工作正常 db_helper.dart的一部分,负责复制数据库并检查它是否已经存在 Future startDB() async { final dbDir

每次我的应用程序第一次启动或数据库被删除时,复制应用程序后会重新启动(如果数据库被删除,但如果是第一次启动,则不会重新启动),并且此错误总是发生:

RangeError(索引):无效值:有效值范围为空:0

之所以会发生这种情况,是因为小部件树上的小部件需要一个列表,但据推测数据库上没有值

但是如果我进行热重启,数据库现在就有了值并且工作正常

db_helper.dart的一部分,负责复制数据库并检查它是否已经存在

 Future startDB() async {
    final dbDir = await getDatabasesPath();
    final dbPath = join(dbDir, "visit_sps.db");

    bool exist = await databaseExists(dbPath);

    if (exist) {
      print("db ja existe");
    } else {
      print("a criar copia dos assets");
      try {
        await Directory(dirname(dbPath)).create(recursive: true);
      } catch (_) {}

      ByteData data = await rootBundle.load("assets/database/visit_sps.db");
      List<int> bytes =
          data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
      await File(dbPath).writeAsBytes(bytes, flush: true);

      print("db copiada");
    }
    openDatabase(dbPath);
  }
Future startDB()异步{
final dbDir=wait getDatabasesPath();
final dbPath=join(dbDir,“visit_sps.db”);
bool exist=等待数据库exists(dbPath);
如果(存在){
打印(“db ja existe”);
}否则{
打印(“抄袭dos资产”);
试一试{
等待目录(dirname(dbPath)).create(递归:true);
}捕获({}
ByteData=wait rootBundle.load(“资产/数据库/访问”_sps.db”);
列表字节=
data.buffer.asUint8List(data.offsetingbytes,data.lengthInBytes);
等待文件(dbPath).writeAsBytes(字节,刷新:true);
打印(“db copiada”);
}
开放数据库(dbPath);
}
我的FutureBuilder和代码,用于在重新安装应用程序且用户设备上仍存在旧数据库的情况下,使用共享首选项包-pagina_principal.dart删除数据库

Future checkFirstSeen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool _seen = (prefs.getBool('seen') ?? false);

    if (_seen) {
      DBHelper().startDB();
    } else {
      await prefs.setBool('seen', true);
      DBHelper().deleteDB();
    }
  }

return FutureBuilder(
      future: DBHelper().getLocais(), //Obter todos os Locais
      builder: (context, AsyncSnapshot<List<Local>> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          List<Local> locais = snapshot.data;
          return Scaffold(
Future checkFirstSeen()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
bool_seen=(prefs.getBool('seen')??false);
如果(看到){
DBHelper().startDB();
}否则{
等待参数setBool('seed',true);
DBHelper().deleteDB();
}
}
回归未来建设者(
future:DBHelper().getLocais(),//Obter todos Locais
生成器:(上下文,异步快照){
if(snapshot.connectionState==connectionState.done){
列表locais=snapshot.data;
返回脚手架(
如果我打印locais长度为0,但在热重启后,现在是数据库中正确的元素数

GetLocais()

Future getLocais()异步{
试一试{
最终数据库=等待数据库;
最终列表映射=等待db.query('local');
返回列表。生成(
地图。长度,
(一){
返回Local.fromMap(maps[i]);
},
);
}捕获(ex){
印刷品(ex);
返回新列表();
}
}
我认为只有在完成getLocais()之后,数据库才能被复制


如果有人能帮助我,我将不胜感激,我还是个新手,如果你不理解我的解释,我很抱歉,因为英语不是我的主要语言!

嗨!我想你需要使用DBHelper().startDB()和wait:
wait DBHelper().startDB();
你好,你能检查一下Mardel答案中的注释吗
 Future<List<Local>> getLocais() async {
    try {
      final db = await database;
      final List<Map<String, dynamic>> maps = await db.query('local');
      return List.generate(
        maps.length,
        (i) {
          return Local.fromMap(maps[i]);
        },
      );
    } catch (ex) {
      print(ex);
      return new List<Local>();
    }
  }