应用终止时无法访问本地数据库

时间:2019-12-26 13:45:17

标签: flutter

我正在使用sqflite 1.2.0 and flutter_background_geolocation`

和带有flutter_background_geolocation的用户无头模式,这使我可以监听位置并按预期工作,但是我尝试比较保存在本地数据库中的当前位置购买位置,如果我关闭了应用程序,但是在打开或在后台运行时都可以使用

  

[ERROR:flutter / lib / ui / ui_dart_state.cc(157)]未处理的异常:MissingPluginException(在com.tekartik.sqflite通道上未找到方法getDatabasesPath的实现)

我的无头代码是

void headlessTask(bg.HeadlessEvent headlessEvent) async {
  print('[BackgroundGeolocation HeadlessTask]: $headlessEvent');
  // Implement a 'case' for only those events you're interested in.
  switch(headlessEvent.name) {
    case bg.Event.LOCATION:
      bg.Location location = headlessEvent.event;
      await LocationListenerHelper.onLocation(location);
      print('- Location: $location');
      break;
    case bg.Event.MOTIONCHANGE:
      bg.Location location = headlessEvent.event;
      await LocationListenerHelper.onLocation(location);
      print('- Location: $location');
      break;
  }
}

void main() async {
  runApp(delegate, MyApp());

  if(Platform.isAndroid){
    // Register your headlessTask:
    bg.BackgroundGeolocation.registerHeadlessTask(headlessTask);
  }
}

onLocation方法代码为

  static Future<void> onLocation(bg.Location location) async {

    List<Map<String, dynamic>> objects = await SqLiteHelper.query(table:'locations');

    if (objects == null || objects.length == 0) {return;}

    print('[new Location listen] ....');

   }

  /*
    init and open database 
  */
  Future<void> databaseInitAndOpen() async {
    // await destroy();
    String path = await databasePath();
    // open the database
    _database = await sqlite.openDatabase(
      path,
      version: 1,
      onCreate: (
        sqlite.Database db,
        int version,
      ) async {
        await db.execute(
            'CREATE TABLE suggestions (id INTEGER PRIMARY KEY, lid Text , title TEXT, counter INTEGER , updatedAt Text)');
        await db.execute('CREATE TABLE itemslocations (id INTEGER PRIMARY KEY, intId INTEGER,type Text, parentId TEXT null, objectId TEXT,lid TEXT, title TEXT, countOfItem INTEGER null, latitude Text, longitude TEXT, radius INTEGER)');
      },
    );

  }

  /* 
    get or query for get data from table
  */
  static Future<List<Map<String, dynamic>>> query({table, where,orderBy,limit}) async {
    List<Map<String, dynamic>> query =
        await _database.query(table, where: where, orderBy: orderBy, limit: limit);
    return query;
  }

0 个答案:

没有答案