【问题标题】:Flutter sqflite works in debug mode on Android Emulator but not on physical device in release modeFlutter sqflite 在 Android Emulator 上的调试模式下工作,但在物理设备上的发布模式下不工作
【发布时间】:2022-01-08 18:24:15
【问题描述】:

我已经使用 Flutter 和 SQFlite 构建了一个应用来持久化数据。我有一个数据库客户端文件,其中有一些关键函数可以让它创建和启动数据库等。

创建数据库:

  /// Creating the database values
  static final DatabaseClient instance = DatabaseClient._init();
  static Database? _database;
  DatabaseClient._init();

调用数据库:

  /// Calling the database
  Future<Database> get database async {
    if (_database != null) return _database!;
    _database = await _initDB('database.db');
    return _database!;
  }

启动或打开数据库:

  /// Future function to open the database
  Future<Database> _initDB(String filePath) async {
    final path = await getDatabasesPath();
    final dbPath = join(path, filePath);
    return await openDatabase(dbPath,
        version: 2, onCreate: _create, onUpgrade: _onUpgrade);
  }

我还创建了一个函数来读取数据库中的所有条目,以使用 ListView 类型的小部件显示:

  /// WB
  Future<List<WBCalcResult>> readAllNotesWB() async {
    final db = await instance.database;
    final orderBy =
        '${WBCalcResultFields.wbDate} DESC ,${WBCalcResultFields.wbTime} DESC';
    final result = await db.query(wbCalcResults, orderBy: orderBy);
    return result.map((json) => WBCalcResult.fromJson(json)).toList();
  }

然后,在显示 ListView 小部件的屏幕中从数据库中获取此数据的函数如下所示:

  /// Function to get the info from the database
  Future<void> refreshWBCalcResults() async {
    setState(() => isLoading = true);
    this.wbCalcResults = await DatabaseClient.instance.readAllNotesWB();
    setState(() {});
    setState(() => isLoading = false);
  }

这一切都可以在 Android Emulator 和 iOS 模拟器上完美运行。

但是,它似乎根本无法在物理 Android 设备上运行(它可能也无法在物理 iOS 设备上运行,尽管我没有测试它的设备)。当我说“它不起作用”时,基本上我所看到的只是一个空白屏幕,ListView 应该在哪里。我看不到错误、应用冻结或类似情况。

我读到这可能是因为 Flutter 应用程序的发布版本中的资源被缩小弄乱了数据库路径查找器,所以我更新了我的 build.gradle 文件以防止资源被缩小:

buildTypes {
    release {
        signingConfig signingConfigs.release
        shrinkResources false
    }
}

遗憾的是,这些似乎都不起作用。我的应用程序的 SQFlite 仍然无法在物理设备上的发布版本中运行,但它仍然在调试模拟器版本中运行。但是,在我发布用于测试的第一个版本中确实如此。

我正在使用所有相关软件包的最新版本,包括 path_provider (2.0.8)

谁能说明为什么我的应用的发布版本会发生这种情况?我该怎么做才能对其进行排序?谢谢。

【问题讨论】:

    标签: android flutter release flutter-provider sqflite


    【解决方案1】:

    对我有帮助的答案:https://stackoverflow.com/a/54267755/2866570

    我在冗长的登录上跑了:flutter run -v -d ...。它准确地显示了导致问题的原因。

    在我的例子中,我在清单文件中更改了我的应用程序的默认包,但没有更改 MainActivity.kt 文件的目录名称。

    错误

    [        ] E/AndroidRuntime(12376): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{io.answer.hello/io.answer.hello.MainActivity}: 
    java.lang.ClassNotFoundException: Didn't find class "io.answer.hello.MainActivity"
    on path: DexPathList[[zip file
    

    我的 git 差异

    diff --git a/android/app/src/main/kotlin/com/example/my_app/MainActivity.kt b/android/app/src/main/kotlin/io/answer/hello/MainActivity.kt
    similarity index 75%
    rename from android/app/src/main/kotlin/com/example/my_app/MainActivity.kt
    rename to android/app/src/main/kotlin/io/answer/hello/MainActivity.kt
    index a310df9..8920150 100644
    --- a/android/app/src/main/kotlin/com/example/my_app/MainActivity.kt
    +++ b/android/app/src/main/kotlin/io/answer/hello/MainActivity.kt
    @@ -1,4 +1,4 @@
    -package com.example.my_app
    +package io.answer.hello
    

    我的AndroidManifest.xml 开头为:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="io.answer.hello">
    

    请注意,我已将此答案中的包名称更改为假的。

    【讨论】:

    • 非常感谢@zaynetro 的输入。碰巧的是,我似乎找到了问题的根源(没有找到解决方案,但至少确定了问题)。这是我的应用程序的第二个版本,与第一个版本相比,我使用了onUpgrade SQFlite 函数来添加一个新表。但是,似乎我这样做的方式是问题所在。当我创建一个新数据库时,并没有尝试更新当前数据库,它工作正常。
    • 看来我需要找到一种方法来更正我使用的onUpgrade 代码,以便将新表添加到现有数据库中。
    猜你喜欢
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多