首页 > 解决方案 > 在页面加载颤动时将 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;
        });
      }

标签: flutterflutter-layout

解决方案


我认为这样更好:

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();
  }

推荐阅读