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.
CodePudding user response:
You can get the data calling the getGroceries() method in initState or using the FutureBuilder widget in flutter.