我的个人资料中有一个共享的数据,其中包含客户的手机号码,当我打开个人资料页面时,需要从分片偏好数据中获取数据,当我将数据加载到文本字段时,该数据需要在文本字段中填写会引发错误
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;
});
}
答案 0 :(得分:1)
我认为这样更好:
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();
}