How to implement queries of database for flutter windows application?

Viewed 317

I have created windows flutter windows application now trying to save local data. I used shared_preferences plugin but it does not support windows applications so I found another package to save local data, sqflite_common_ffi plugin. But don't know how to use it. I followed it's example and it worked but now want to separate it's all functionalities like I want to create a database in a method , and insert values in another method, get values in another method and delete them in another method.

CODE

import 'package:sqflite_common/sqlite_api.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
class Storages{
  
  var databaseFactory = databaseFactoryFfi;
  Database db; 

  createDatabase ()async{
    // Init ffi loader if needed.
    sqfliteFfiInit();
    db = await databaseFactory.openDatabase(inMemoryDatabasePath);
  
        await db.execute('''
          CREATE TABLE Credential(
              id INTEGER PRIMARY KEY,
              token TEXT,
              password TEXT
          )
        ''');
    
    
  }

  saveToken(String token)async{
   await db.insert('Credential', <String, dynamic>{'token': token});
  }

  savePassword(String password)async{
    await db.insert('Credential', <String, dynamic>{'password': password});
  }

  getToken()async{ 
    var result = await db.query('Credential');
    print('Credential token database;$result');
   
   //  await db.close();
  }

  

  getPassword()async{  
    var result = await db.query('Credential');
    print('Credential password database;$result');
    
  }

}

then first I initialized database saved the password in database from login screen but when I restart my application and I called getpassword method from initState of my splash screen.

 Storages storage = new Storages();

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

and thrown me this exception in my terminal

 Unhandled Exception: NoSuchMethodError: The method 'query' was called on null.
Receiver: null
Tried calling: query("Credential")
 
0 Answers
Related