sqlitedartfluttersqlite-json1

Loading JSON1 extension with Flutter sqflite


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.

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.


Solution

  • 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.