flutter - Flutter:“SharedPreferences”的实例
问题描述
我在我的 Flutter 应用程序中使用 shared_preferences 插件,我想在用户选择城市时保存数据。但是当我尝试打印时,它只是说;
“SharedPreferences”未处理异常的实例:setState() 回调参数返回了 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);
});
},
推荐阅读
- python - 使用 scikit-learns SimpleImputer 时输入错误
- javascript - 学生注册挂钩后如何在wordpress中的“添加新用户”表单上注册家长
- c# - 当另一个选项卡禁用时,如何使用 XAML 触发器更改 TabControl 索引?
- python-3.x - 无法使用美汤从网页中提取 div 标签?
- javascript - 测试 Redux 连接的 Typescript 文件
- c# - 在 Collections 和 Regex.Match 方法之间传递数据
- android - ViewPager 有两个包含一组卡片的片段
- c# - 方法应该等到另一个方法启动的另一个方法完成
- php - Jquery获取请求不显示数据
- javascript - Web组件稳定状态后如何添加样式?