Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite 如何在ChangeNotifier中使用Futures?_Sqlite_Flutter_Dart_Flutter Provider - Fatal编程技术网

Sqlite 如何在ChangeNotifier中使用Futures?

Sqlite 如何在ChangeNotifier中使用Futures?,sqlite,flutter,dart,flutter-provider,Sqlite,Flutter,Dart,Flutter Provider,我有一个sqlite数据库,可以从中读取数据。我还有一个很长的小部件树。因此,经过一点研究,我找到了提供者颤振包。但是我不知道如何在类中使用Futures,扩展ChangeNotifier,或者如何在我的小部件树中的任何地方使用它 class MyProvider和ChangeNotifier{ 动态值; 动态获取myValue=>\u myValue; 设置myValue(动态newValue){ _myValue=newValue; notifyListeners(); } MyProvid

我有一个
sqlite
数据库,可以从中读取数据。我还有一个很长的小部件树。因此,经过一点研究,我找到了
提供者
颤振包。但是我不知道如何在类中使用Futures,扩展
ChangeNotifier
,或者如何在我的小部件树中的任何地方使用它

class MyProvider和ChangeNotifier{
动态值;
动态获取myValue=>\u myValue;
设置myValue(动态newValue){
_myValue=newValue;
notifyListeners();
}
MyProvider(){
loadValue();
}
未来加载值异步{
myValue=await db.instance().getValue();
}
未来重蚀刻异步{
myValue=await db.instance().newValue();
notifyListeners();
}
}

我建议您先去了解Futures和Provider软件包的工作原理。您可以使用.then()方法直接在小部件树中等待将来完成,也可以在提供程序中这样处理:

class MyProvider with ChangeNotifier {
  dynamic _myValue;

  dynamic get myValue => _myValue;

  set myValue(dynamic newValue) {
    _myValue = newValue;
    notifyListeners();
  }

  MyProvider(){
    loadValue();
  }

  Future<void> loadValue async {
   myValue = await db.instance().getValue();
  }
}
class MyProvider和ChangeNotifier{
动态值;
动态获取myValue=>\u myValue;
设置myValue(动态newValue){
_myValue=newValue;
notifyListeners();
}
MyProvider(){
loadValue();
}
未来加载值异步{
myValue=await db.instance().getValue();
}
}
然后在小部件树中:

build(context) {
  return ChangeNotifierProvider.value(
    value: MyProvider(),
    child: Consumer<MyProvider>(builder:
      (BuildContext context, MyProvider provider, Widget child) {
       if(provider.myValue == null) {
         return Center(
            child: CircularProgressIndicator(),
         );
       } else {
          return Container(
            child: Text(provider.myValue);
          );
       }
    }
  );
}
build(上下文){
返回ChangeNotifierProvider.value(
值:MyProvider(),
子项:消费者(建筑商:
(BuildContext上下文、MyProvider提供程序、小部件子项){
if(provider.myValue==null){
返回中心(
子对象:CircularProgressIndicator(),
);
}否则{
返回容器(
子级:文本(provider.myValue);
);
}
}
);
}

这只是一种方法,但是还有很多其他的方法来处理这个问题。

我想你知道如何使用
未来的
。像我在上面编辑的问题中所做的那样创建refetch函数是否有效…??在
loadData()的末尾添加了
notifyListeners();
);
现在可以正常工作了您已经有了notifyListeners();在myValue的setter中,您不需要在获取方法中再次使用