I was working on local storage in flutter using SQflite db and Used Future to extract the file from the db to display using ListTile but it does't update instantly like stream do when I insert a new value to the db.
// This method is from the database to get the tasks that has been entered!
{
Future<List<Model>> getTasks() async {
Database _db = await database();
List<Map<String, dynamic>> taskMap = await _db.query('tasks');
return List.generate(taskMap.length, (index) {
return Model(
id: taskMap[index]['id'],
name: taskMap[index]['name'],
fatherName: taskMap[index]['fatherName']);
});
}
}
```
// This is Future Builder to extract the data from the database
{
Expanded(
child: FutureBuilder(
future: _dbHelper.getTasks(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: 3,
itemBuilder: (context, index) {
return ContactList(snapshot.data[index], index);
},
);
},
),
)
}
```
// This is the answer
{
// this is method in provider class to get the task inserted in the db
Future loadTaskList() async {
_isLoading = true;
notifyListeners();
_taskList = await db.getTasks();
_isLoading = false;
notifyListeners();
}
}
// call the provider class in the main.dart file like this
{
ChangeNotifierProvider(
create: (ctx) => InputData()..loadTaskList(),
),
}
// then just use Consumer or Provider when you access the methods.
**This work perfectly for me!**