Loading JSON1 extension with Flutter sqflite

1.3k views Asked by At

In my Flutter app I'm using sqflite to talk to a local database. I need to peek into JSON data. The JSON1 extension would be ideal for this. However, I can't load the extension in a Flutter app to make it available in my queries since the documentation is for C, not Dart.

  • Should I compile in different architectures for iOS and Android?
  • Where should the compiled file(s) be placed? I assume it's not added as an asset.
  • How to access/load the C extension in my Flutter/Dart code?

Suggestions for other well supported local databases (document databases or databases that support JSON queries) for Flutter are also welcome. I looked into Couchbase Lite, but the plugins (Fluttercouch, couchbase-lite-flutter) are still under development.

1

There are 1 answers

0
Ralf On BEST ANSWER

By default, sqflite uses the OS-supplied version of SQLite, which may not have json1 enabled.

Instead, use sqflite_ffi, which would ship a copy of SQLite with your application. Any recent version would include json1 automatically.

The process is explained here: https://github.com/tekartik/sqflite/blob/master/sqflite_common_ffi/doc/using_ffi_instead_of_sqflite.md

The gist is:

  1. Add sqflite_common_ffi and sqlite3_flutter_libs dependencies (in addition to sqflite).
  2. Call sqfliteFfiInit() before opening the database sqflite in your app.

An added advantage is that the ffi version also has much better performance.