I insert SQLite database file in my project and its size almost 2GB. I always got this error message in debug console when running on Android native device:
E/flutter (25145): [ERROR:flutter/shell/common/shell.cc(117)] Dart Error: NewExternalTypedData expects argument 'length' to be in the range [0..1073741823].
After googling a long time and read this post I understood, I can't load database file like this size with my method:
Future<Database?> get database async {
if (_db != null) return _db;
Directory applicationDirectory = await getApplicationDocumentsDirectory();
String dbPath = join(applicationDirectory.path, "library.db");
bool isDBExists = await File(dbPath).exists();
if (!isDBExists) {
ByteData database = await rootBundle.load(join("assets", "database/library.db"));
List<int> bytes = database.buffer.asUint8List(database.offsetInBytes, database.lengthInBytes);
await File(dbPath).writeAsBytes(bytes, flush: true);
}
_db = await openDatabase(dbPath);
return _db;
}
I knew after searching and googling these lines aren't working to load large database:
ByteData database = await rootBundle.load(join("assets", "database/library.db"));
List<int> bytes = database.buffer.asUint8List(database.offsetInBytes, database.lengthInBytes);
await File(dbPath).writeAsBytes(bytes, flush: true);
And I tried change List<int> to List<Int64> and Uint8List still can't load database and same error!
pubspec.yaml:
dependencies:
path: ^1.9.0
path_provider: ^2.1.3
sqflite: ^2.3.3+1
flutter:
assets:
- assets/database/library.db
How can I fix this error and load my large database normally?
ByteData databaseto store 2 GB of data in the RAM memory? what is the physical device you want to run that code?assets/database/library_chunk000.db, assets/database/library_chunk001.db, ...where each chunk has ~10 MB or so - in such case you would need to load ~200 chunks