Reputation: 2504
I'm trying to load prepopulated data into my flutter application. I've created 'assets' folder in the root of my project & put 'mydb.sql' file into that folder.
Added that file reference in pubspec.yaml
assets:
- assets/mydb.sql
Below is my code of DBHandler.dart file to access database
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));
Upvotes: 3
Views: 4304
Reputation: 2504
There were 2 issues in my code. Writing this to help others doing the same mistake.
1. Indentation in 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
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. 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:')
2. sql file is not db So, I got this issue after solving the first one. I got mydb.sql is not a database. So I exported my database as '.sqlite' file from Sqlite Database Browser. & updated my pubspec & DBHandler file.
Thanks to @Shababb Karim's comment for pointing out pubspec.yaml issue.
Upvotes: 4