简体   繁体   English

如何从Future对象中获取原始值(未来 <int> )扑朔迷离?

[英]How to get primitive value from Future object (Future<int>) in flutter?

I am getting Future instance while getting sharedPreference value as follows 我在获取sharedPreference值时正在获取Future实例,如下所示

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

Then I want to get int value from that Future instance. 然后,我想从该Future实例获取int值 Please help thanks in advance. 请提前帮助谢谢。

There is no way to get back from async execution to sync execution. 无法从异步执行恢复到同步执行。

You can only get the value by using await or .then((param) { print(param); }) 您只能使用await.then((param) { print(param); })来获取值.then((param) { print(param); })

You would need to provide more context to make a concrete suggestion about how to solve your actual problem. 您需要提供更多的上下文,以提出有关如何解决实际问题的具体建议。

For non-async method: 对于非异步方法:

void methodSync() {
  getCounterValue().then(
    (value){
      // do something with value
      print(value);
    }
  );
}

For async method: 对于异步方法:

void methodAsync() async{
  int value = await getCounterValue();
  // do something with value
  print(value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM