Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/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
Dart 如何在颤振中从未来对象(Future)获取原始值?_Dart_Flutter - Fatal编程技术网

Dart 如何在颤振中从未来对象(Future)获取原始值?

Dart 如何在颤振中从未来对象(Future)获取原始值?,dart,flutter,Dart,Flutter,我在获取SharedReference值的同时获取Future实例,如下所示 Future<int> getCounterValue() async{ SharedPreferences preferences= await SharedPreferences.getInstance(); return preferences.getInt("counter") ; } 然后我想从未来的实例中获取int值。请提前感谢您的帮助。无法从异步执行返回到同步执行

我在获取SharedReference值的同时获取Future实例,如下所示

 Future<int>  getCounterValue() async{
    SharedPreferences preferences= await SharedPreferences.getInstance();
    return preferences.getInt("counter") ;
   }

然后我想从未来的实例中获取int值。请提前感谢您的帮助。

无法从异步执行返回到同步执行

只能使用wait或.thenparam{printparam;}获取值


您需要提供更多的上下文,以便就如何解决实际问题提出具体建议。

无法从异步执行返回到同步执行

只能使用wait或.thenparam{printparam;}获取值

您需要提供更多的上下文,就如何解决实际问题提出具体建议。

对于非异步方法:

对于异步方法:

对于非异步方法:

对于异步方法:


感谢我通过使用.thenparam{printparam;}方法得到的结果,但正如我们在android java中所做的那样,当从共享偏好中获得价值时,它直接涉及到价值。在Flatter中是否有任何方法可以做到这一点。没有这种方法。从Flatter到本机平台的所有通信都是异步的,因此共享首选项也必须是异步的。感谢我使用了.thenparam{printparam;}方法,但正如我们在android java中所做的那样,当从共享首选项中获取值时,它直接涉及到值。在Flatter中是否有任何方法可以做到同样的事情。没有这种方法。从Flatter到本机平台的所有通信都是异步的,因此共享首选项也必须是异步的。
void methodSync() {
  getCounterValue().then(
    (value){
      // do something with value
      print(value);
    }
  );
}
void methodAsync() async{
  int value = await getCounterValue();
  // do something with value
  print(value);
}