【发布时间】: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