flutter - 在页面加载颤动时将 sharedprefrence 数据加载到 Textfield
问题描述
我有一个共享数据,其中包含客户的移动号码,在我的个人资料中,当我打开个人资料页面时需要在文本字段中填写,我从碎片偏好数据中获取数据,当我将数据加载到文本字段时它会抛出错误
TextEditingController mobile = TextEditingController();
void initState() {
getMobile();
}
从 Sharedpreference 获取数据
Future<String> getMobile() async {
Future notificatinstatus = SharedPrefrence().getUserMobile();
notificatinstatus.then((data) async {
var mobile_no=data;
mobile_no.text=mobile;
return mobile;
});
}
解决方案
我认为这样更好:
var mobileController = TextEditingController();
getMobile() async {
Future notificatinstatus = SharedPrefrence().getUserMobile();
notificatinstatus.then((data) async {
var mobile_no=data;
setState(() {
if(mobile_no.isNotEmpty){
mobileController.text = mobile_no;
}
});
});
}
@override
void initState() {
super.initState();
getMobile();
}
推荐阅读
- spring-boot - 如何在 Spring Boot 下从 Thymeleaf 调用 @Service bean
- python - 在 django admin 中显示 img
- node.js - node forever 命令什么也不显示
- spring - 在 MariaDB 中执行多个 SQL 查询
- c - 多线程服务器客户端 C
- swift - 如何检查一个节点是否靠近另一个节点?
- json - packer 用户变量中的对象
- google-chrome - Chrome 71 - Flash 和 Cookie
- unity3d - 如何通过脚本忽略投影仪中的第 20 层和第 9 层?
- reactjs - create-react-app --typescript:提供“--isolatedModules”标志时无法编译命名空间