gpt4 book ai didi

flutter : 'SharedPreferences' 的实例

转载 作者:行者123 更新时间:2023-12-04 08:56:48 24 4
gpt4 key购买 nike

我在 Flutter 应用程序中使用了 shared_preferences 插件,我想在用户选择城市时保存数据。
但是当我尝试打印它时,它只是说;

Instance of 'SharedPreferences' Unhandled Exception: setState()callback argument returned a Future


. (即使我删除了我的 setState 部分,我也会在我的控制台中得到同样的错误)。
有谁知道原因??
我在卡片小部件中的文字
 Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8.0),
child: Text(
snapshot.data.name,
style: TextStyle(fontSize: 16),
),
),
当点击我保存它的地方时
   onTap: () {
setState(() async {
final country =
await SharedPreferences.getInstance();
String name;
name=snapshot.data.name;
country.setString('name', name);
print('here $country');

});
},

最佳答案

尝试

 onTap: () async {
final country =
await SharedPreferences.getInstance();
setState(() {
String name;
name=snapshot.data.name;
country.setString('name', name);
print('here $name);

});
},

关于 flutter : 'SharedPreferences' 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63776782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com