如何在flutter中的异步函数上返回值? [英] How to return value on async function in flutter?
本文介绍了如何在flutter中的异步函数上返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
SharedPreferences sharedPreferences;
token() async {
sharedPreferences = await SharedPreferences.getInstance();
return "Lorem ipsum dolor";
}
打印时,我在调试控制台上收到了此消息
When I print, I got this message on debug console
Instance of 'Future<dynamic>'
如何获取 lorem ipsum ...字符串?非常感谢
How I can get string of "lorem ipsum..." ? thank you so much
推荐答案
token()
是异步的,这意味着它返回未来
。您可以这样获得值:
token()
is async which means it returns Future
. You can get the value like this:
SharedPreferences sharedPreferences;
Future<String> token() async {
sharedPreferences = await SharedPreferences.getInstance();
return "Lorem ipsum dolor";
}
token().then((value) {
print(value);
});
但是有一种更好的使用SharedPreferences的方法。在此处查看文档。
But there is a better way to use SharedPreferences. Check docs here.
这篇关于如何在flutter中的异步函数上返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文