0

How can get SQLite date in flutter?

In my project, I had used the sqlite and I had written the code, but I don't know how can get data when I need to read it.

class DatabaseHelper {
  DatabaseHelper._privateConstructor();
  static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
  static Database? _database;
  Future<Database> get database async => _database ??= await _initDatabase();

  Future<Database> _initDatabase() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, 'user.db');
    return await openDatabase(path, version: 1, onCreate: _onCreate);
  }

  Future _onCreate(Database db, int version) async {
    await db
        .execute('''CREATE TABLE USER (id INTEGER PRIMARY KEY, badge TEXT)''');
  }

  Future<List<Grocery>> getGroceries() async {
    Database db = await instance.database;
    var groceries = await db.query('USER', orderBy: 'badge');
    List<Grocery> groceryList = groceries.isNotEmpty
        ? groceries.map((e) => Grocery.fromMap(e)).toList()
        : [];

    return groceryList;
  }
}

I had written this code and I need to got the badge data. How could I can got it?

The bage data only has one date, it was just recode the notification badge. so anytime was update it.

1

1 Answer 1

0

You can get the data calling the getGroceries() method in initState or using the FutureBuilder widget in flutter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.