5

I am searching for a plugin or any solution that make SQLite work on flutter desktop windows app, i tried sqflite plugin and it work well with macOS desktop app but it doesn't support windows.

Nadim Gouia
  • 141
  • 1
  • 2
  • 7

6 Answers6

9

I had exactly same problem and used https://pub.dev/packages/sqflite_common_ffi, it works like a charm

AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210
  • 2
    Yes this is the correct method for using sqflite on windows, see link below for easy implementation https://github.com/tekartik/sqflite/blob/master/sqflite_common_ffi/doc/using_ffi_instead_of_sqflite.md – mister_cool_beans Mar 31 '22 at 07:02
7

Sqflite is not available on desktop but you can use moor which is very close to Sqflite, according to the documentation: moor works on Android, iOS, macOS, Windows, Linux & Web

I advise you to use it to have a real support on all platforms very easily.

huextrat
  • 402
  • 3
  • 11
  • am trying moor with windows but i cant get auto generated classes like _$AppDatabase is there any additional configuration ? – Nadim Gouia Feb 11 '20 at 15:45
2

You can try ObjectBox. https://github.com/objectbox/objectbox-dart

  1. Add dependencies to your project
  2. On windows, run bash <(curl -s https://raw.githubusercontent.com/objectbox/objectbox-dart/main/install.sh)
  3. It will download objectbox.dll, put it to path your-project\build\windows\runner\Debug\ or \Release
  4. enjoy!
ruoyu_2021
  • 21
  • 3
  • For sqlite, you can try https://pub.dev/packages/sqflite_common_ffi 1. download sqlite.dll from https://www.sqlite.org/download.html 2. put sqlite.dll to debug or release dirctory 3. enjoy! But the db file will be generated in the wrong directory. I suggest you use ObjectBox. – ruoyu_2021 Jan 13 '21 at 21:39
2

drift is a new package for moor as moor is discontinued. Drift works on Android, iOS, macOS, Windows, Linux and the web

ganiular
  • 331
  • 2
  • 8
0

There is an issue about that you can follow here: https://github.com/tekartik/sqflite/issues/356

Windows plugin support is not stable yet but there is an experimental support for windows using ffi (and actually the moor_ffi implementation) here: https://github.com/tekartik/sqflite_more/tree/master/sqflite_ffi_test

alextk
  • 5,713
  • 21
  • 34
  • i tried both sqflite and moor_ffi and no one of them work on windows – Nadim Gouia Feb 12 '20 at 16:26
  • Have you tried the following sample: https://github.com/tekartik/sqflite_more/tree/master/sqflite_test_app (note that you have to be on flutter master on windows) – alextk Feb 12 '20 at 17:09
0

SQFlite on Windows

Step - 1

Remember to Download Windows Binary Download SQLite DDL FIle

Path

Add the DLL File to this path sqlpath

Code

class SQFLite {
  Database? _database;

  Future<Database> get database async {
    if (_database != null) {
      return _database!;
    }
    _database = await initWinDB();
    return _database!;
  }

  Future<Database> initWinDB() async {
    sqfliteFfiInit();
    final databaseFactory = databaseFactoryFfi;
    return await databaseFactory.openDatabase(
      inMemoryDatabasePath,
      options: OpenDatabaseOptions(
        onCreate: _onCreate,
        version: 1,
      ),
    );
  }

// based on what type of data you want to add your Table
  Future<void> _onCreate(Database database, int version) async {
    final db = database;
    await db.execute(""" CREATE TABLE IF NOT EXISTS users(
              id INTEGER PRIMARY KEY,
              name TEXT,
              email TEXT,
              password INTEGER,
              phoneNumber INTEGER
            )
  """);
  }
}

Initialization

Either you can write this in the main function or if you are working on separate file You can add it inside initState()

  void initState() {
    super.initState();
    WidgetsFlutterBinding.ensureInitialized();
    helper.initWinDB();
  }

Github Code

Ayoub Ali
  • 91
  • 1
  • 4