6

I am trying to load SQLite DB in Flutter for Windows (it's working on Android), but I receive an exception, my code is :

// Copy from asset
ByteData data = await rootBundle.load(join("assets", "mydb.sqlite"));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);

I receive this exception :

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Unable to load asset: assets\mydb.sqlite #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7) #1 DatabaseHelper._initDatabase (package:myapp/database_helper.dart:54:11) #2 DatabaseHelper.database (package:myapp/database_helper.dart:29:17) #3 DatabaseHelper.queryAllRows (package:myapp/database_helper.dart:80:19) #4 _MyHomePageState._query (package:myapp/main.dart:193:15)

AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210

2 Answers2

0

Assets paths in flutter windows also use unix separator / not \. Don't use path.join() to construct asset path and instead use yourStringList.join('/')

Hammerhead
  • 1,044
  • 8
  • 19
0

I came across the same error. I was able to fix it by changing

ByteData data = await rootBundle.load(join("assets", "mydb.sqlite"));

to

ByteData data = await rootBundle.load("assets/mydb.sqlite");

The parameter passed to the load function should exactly represent the path provided in the pubspec.yaml file (seems like the join function twists the /to a \ on windows)

assets:
    - assets/mydb.sqlite
James
  • 87
  • 1
  • 1
  • 8