在 flutter 中使用 Future 方法时即时更新 SQflite 数据库

Instant update of SQflite db when using Future methods in flutter

我正在使用 SQflite db 在 flutter 中处理本地存储,并使用 Future 从数据库中提取文件以使用 ListTile 显示,但是当我向数据库插入新值时它不会像流那样立即更新。

//该方法是从数据库中获取已经录入的任务!

            {
            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!**