在iOS上,setState在Android上的运行效果不如Android

时间:2019-05-12 09:29:53

标签: ios flutter setstate

我正在共享偏好设置下执行一些收费信息,但在iOS中却无法正常工作,而在Android中则按预期工作

@override
void initState() {
    super.initState();
    readData();
}

当readData()有数据时,我有2个文本字段用于填充

final email = TextFormField(
      keyboardType: TextInputType.emailAddress,
      autofocus: false,
      initialValue: _email,
      validator: (input) {
        if(input.isEmpty) {
          return 'Introduce un Email';
        }
      },
      onSaved: (input) => _email = input,
      decoration: InputDecoration(
          hintText: 'Email',
          contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
          border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);

final password = TextFormField(
      autofocus: false,
      obscureText: true,
      initialValue: _password,
      validator: (input) {
        if(input.isEmpty) {
          return 'Introduce la contraseña';
        }
      },
      onSaved: (input) => _password = input,
      decoration: InputDecoration(
          hintText: 'Contraseña',
          contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
          border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
readData() async {
    final prefs = await SharedPreferences.getInstance();

    setState(() {
      _email = prefs.getString('email');
      _password = prefs.getString('password');
    });
}

在android中效果很好,但在iOS中效果不佳

1 个答案:

答案 0 :(得分:0)

我认为更新文本字段值的正确方法是使用TextEditingController。 您的代码如下所示:

您所在的州

final TextEditingController emailCtrl = TextEditingController();
final TextEditingController passwordCtrl = TextEditingController();

readData() async {
    final prefs = await SharedPreferences.getInstance();
    emailCtrl.text = prefs.getString('email');
    password.text = prefs.getString('password');
}

您的文本字段

final email = TextFormField(
     // ... your other arguments
    controller: emailCtrl
);
final password = TextFormField(
     // ... your other arguments
    controller: passwordCtrl
);