【发布时间】:2021-10-22 12:50:23
【问题描述】:
几天来一直在研究此代码,试图学习如何在颤振项目中正确地与 SQFLite 交互,在我的编码过程中,我遇到了一个错误并检查了整个 StackOverflow,我什至用谷歌搜索了它但没有答案.虽然我在 StackOverflow 上发现了一个类似问题的问题,但没有给出答案(很抱歉,如果我问这个问题,因为已经存在一个问题,但因为它没有得到回答)
正在做什么
我实际上是在尝试从互联网上提取数据并将其直接保存到本地存储 (sqflite),所以每次我运行我的代码时,数据都会插入到本地存储中,但我会收到关于特定行的错误
我的代码
DatabaseHelper 类
class DatabaseHelper {
DatabaseHelper._createInstance();
static DatabaseHelper databaseHelper = new DatabaseHelper._createInstance();
static Database? _database = null;
static final _databaseName = 'prodcuts.db';
static final _databaseVersion = 1;
String productTable = 'product_table';
String colId = 'id';
String colTitle = 'title';
String colUrl = 'url';
String colThubmnail = 'thumbnailUrl';
String colalbumId = 'albumId';
Future<Directory?>? _appDocumentDirectory;
factory DatabaseHelper() {
if (databaseHelper != null) {
databaseHelper = DatabaseHelper._createInstance();
}
return databaseHelper;
}
// Future<Database> get database async {
// print('Learning App 1 - ${_database}');
// var ini = await initializeDatabase();
// print('Learning App 2 -${ini}');
// return _database ??= await initializeDatabase();
// }
Future<Database> get database async {
if (_database != null) {
return _database!;
}
_database = await initializeDatabase();
return _database!;
}
Future<Database> initializeDatabase() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = join(directory.path, _databaseName);
print("Learning App 3 -${directory}");
var productsDatabase = await openDatabase(path,
version: _databaseVersion, onCreate: _createDb);
return productsDatabase;
}
void _createDb(Database db, int newVersion) async {
await db
.execute('CREATE TABLE $productTable($colId INTEGER, $colTitle TEXT, '
'$colUrl TEXT, $colThubmnail TEXT, $colalbumId INTEGER )');
}
Future<List<Map<String, dynamic>>> getProductMapList() async {
Database db = await databaseHelper.database;
var result = await db.query(productTable, orderBy: '$colId ASC');
return result;
}
// Insert Operation: Insert a Note object to database
// Future<int> insertProduct(Product product) async {
// Database db = await this.database;
// var result = await db.insert(productTable, product.toJson());
// return result;
// }
Future<List<Product>> insertProduct(List<Product> product) async {
Database db = await databaseHelper.database;
product.forEach((element) async {
await db.insert(productTable, element.toJson());
});
print('was inserted');
return product;
}
Future<int> updateProduct(Product product) async {
var db = await databaseHelper.database;
var result = await db.update(productTable, product.toJson(),
where: '$colId = ?', whereArgs: [product.id]);
return result;
}
Future<int> deleteProduct(int id) async {
var db = await databaseHelper.database;
int result =
await db.rawDelete('DELETE FROM $productTable WHERE $colId = $id');
return result;
}
Future<int?> getCount() async {
Database db = await databaseHelper.database;
List<Map<String, dynamic>> x =
await db.rawQuery('SELECT COUNT (*) from $productTable');
int? result = Sqflite.firstIntValue(x);
return result;
}
Future<List<Product>> getProductList() async {
var productMapList = await getProductMapList();
int count = productMapList.length;
var productList = <Product>[];
for (int i = 0; i < count; i++) {
//print('yes --${productMapList[i]}');
productList.add(Product.fromJson(productMapList[i]));
}
return productList;
}
}
得到了什么
我从这行得到一个错误
Directory directory = await getApplicationDocumentsDirectory();
到目前为止做了什么
- 已删除应用并重新安装
- 已降级 sqflite 和 path_provider 插件依赖项
- 使缓存无效并重新启动
- 飘飘然
- 在我的代码中添加了 hive_flutter
Future<void> main() async {
Get.put(ProductController());
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
runApp(MyApp());
}
我的依赖
intl: ^0.17.0
sqflite: ^2.0.0+4
path_provider: ^2.0.5
hive_flutter: ^1.1.0
我显然不知道做错了什么,但我真的需要帮助来解决它
在我忘记之前,这是我的颤振医生-v
[✓] Flutter (Channel stable, 2.5.3, on macOS 11.4 20F71 darwin-x64, locale en-US)
• Flutter version 2.5.3 at /Users/apple/Developer/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 18116933e7 (7 days ago), 2021-10-15 10:46:35 -0700
• Engine revision d3ea636dc5
• Dart version 2.14.4
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Users/apple/Library/Android/sdk
• Platform android-30, build-tools 30.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.5, Build version 12E262
• CocoaPods version 1.10.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2020.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
???? https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
???? https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
[✓] Connected device (2 available)
• iPhone 12 Pro Max (mobile) • 3D408340-B566-4B0C-A9C3-161B148ECC4A • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
• Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.81
! Doctor found issues in 1 category.
【问题讨论】: