0

I'm new on SQLite and flutter. I'm trying to access an SQLite database (which is on my computer in the project folder at '~/Work/FlutterTest/ProjectTestFlutter/poa.db'.

But I have null when I try to cast my List to String.

There is my code :

class _DbPageState extends State<DbPage> {
  String data;
  var databasesPath;
  String path;
  Database database;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: Container(
      child: Row(
        children: <Widget>[
          new IconButton(
              onPressed: () {
                data = getRecords().toString();
              },
              icon: Icon(Icons.more_vert)),
          new Text(
            data,
          )
        ],
      ),
    ));
  }

  Future<void> openDb() async {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, '~/Work/FlutterTest/ProjectTestFlutter/poa.db');

    database = await openDatabase(path, version: 1);
  }

  Future<List<Map>> getRecords() async {
    List<Map> list = await database.rawQuery('SELECT * FROM tbl1');
    return list;
  }

  Future<void> closeDb() async {
    await database.close();
  }
}
6
  • Is the database being opened correctly? Because in order to open a preinitialized db in Flutter, you need to copy it to the device from the assets folder in the first place. You are getting the path as a computer path, which should not work. Commented Apr 30, 2019 at 10:50
  • so you are trying to access a db file that is on your computer from an app running from your device? Commented Apr 30, 2019 at 10:50
  • getDatabasesPath will give you a path in your device. You should make your db file path something like databasesPath + '/poa.db'. But again, you should copy it first from the flutter assets bundle to that path. Commented Apr 30, 2019 at 11:05
  • @Martyns oh so no it not opened correctly though. Could you provide an answer, please? Commented Apr 30, 2019 at 11:06
  • @Ryosuke looks like a problem indeed. Commented Apr 30, 2019 at 11:06

1 Answer 1

1

When app starts for example, copy the db to your device.

ByteData data = await rootBundle.load("assets/poa.db");
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);

var databasesPath = await getDatabasesPath();
String dbPath = join(databasesPath, "poa.db");
await File(dbPath).writeAsBytes(bytes);

Remember to add the db file to the pubspec.yml, just as you would do with image resources. After that you could open it from the widget with:

var databasesPath = await getDatabasesPath();
String dbPath = join(databasesPath, "poa.db");
var database = await openDatabase(dbPath, version: 1);
Sign up to request clarification or add additional context in comments.

1 Comment

@Itoun That is great!

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.