0

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?

6
  • you want ByteData database to store 2 GB of data in the RAM memory? what is the physical device you want to run that code? Commented Jun 6, 2024 at 10:23
  • Yes, I want to run this code just in Android devices because I specified my project just for Android OS and I ran this code on my device (Samsung Galaxy A22). @pskink Commented Jun 6, 2024 at 10:31
  • 2
    even thou galaxy a22 has 6GB of RAM memory, it imposes per-app memory limits to ensure system stability and performance, so i am pretty sure you will never be able to load 2GB data in one shoot, most likely you need to divide it into 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 Commented Jun 6, 2024 at 10:56
  • How can I use chunks? I never used it before! Could solve it please and explain how it works? @pskink Commented Jun 6, 2024 at 11:11
  • 1
    in a loop, read all the chunks in a loop and append each chunk to the output file Commented Jun 6, 2024 at 11:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.