[英]Unable to read sql file from assets in flutter
I'm trying to load prepopulated data into my flutter application. 我正在尝试将预先填充的数据加载到我的flutter应用程序中。 I've created 'assets' folder in the root of my project & put 'mydb.sql' file into that folder. 我已经在项目的根目录中创建了“ assets”文件夹,并将“ mydb.sql”文件放入该文件夹中。
Added that file reference in pubspec.yaml 在pubspec.yaml中添加了该文件引用
assets:
- assets/mydb.sql
Below is my code of DBHandler.dart file to access database 下面是我访问数据库的DBHandler.dart文件的代码
static Database _db;
String dbName = "mydb.sql";
Future<Database> get db async {
if (_db != null) return _db;
_db = await initDb();
return _db;
}
initDb() async {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, dbName);
var exists = await databaseExists(path);
if (!exists) {
// Should happen only the first time you launch your application
print("Creating new copy from asset");
// Make sure the parent directory exists
try {
await io.Directory(dirname(path)).create(recursive: true);
} catch (_) {}
// Copy from asset
ByteData data = await rootBundle.load(join('assets',dbName));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Write and flush the bytes written
await io.File(path).writeAsBytes(bytes, flush: true);
} else {
print("Opening existing database");
}
return await openDatabase(path);
}
the error I'm getting is 我得到的错误是
I/flutter (16900): Creating new copy from asset
E/flutter (16900): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Unable to load asset: assets/mydb.sql
E/flutter (16900): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
E/flutter (16900): <asynchronous suspension>
E/flutter (16900): #1 DBHandler.initDb (package:MyApp/db/DBHandler.dart:36:40)
which is below given line from code. 在代码的给定行下面
ByteData data = await rootBundle.load(join('assets',dbName)); ByteData数据=等待rootBundle.load(join('assets',dbName));
There were 2 issues in my code. 我的代码中有2个问题。 Writing this to help others doing the same mistake. 写这个可以帮助其他人犯同样的错误。
1. Indentation in pubspec.yaml 1. pubspec.yaml中的缩进
I was doing a major silly mistake. 我犯了一个重大的愚蠢错误。 I was just looking at 我只是看着
assets:
- assets/mydb.sql
my pubspec file was something was like this 我的pubspec文件是这样的
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/mydb.sqlite
I didn't notice that my 'flutter:' & 'assets:' was on the same level. 我没有注意到我的“ flutter:”和“ assets:”处于同一水平。 so I changed it to 所以我改成
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/mydb.sqlite
Notice the indentation (2 spaces before 'assets:') 注意缩进(“ assets:”之前的2个空格)
2. sql file is not db So, I got this issue after solving the first one. 2. sql文件不是db,所以在解决第一个问题后出现了这个问题。 I got mydb.sql is not a database. 我得到了mydb.sql不是数据库。 So I exported my database as '.sqlite' file from Sqlite Database Browser. 因此,我从Sqlite数据库浏览器将数据库导出为“ .sqlite”文件。 & updated my pubspec & DBHandler file. 并更新了我的pubspec和DBHandler文件。
Thanks to @Shababb Karim's comment for pointing out pubspec.yaml issue. 感谢@Shababb Karim的评论指出了pubspec.yaml问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.