如果在我的文本表单字段上创建了一个初始值,我正在尝试设置一个初始值。当我运行我的代码时:
final apiField = TextFormField(
controller: apiFieldController,
initialValue: _read().toString(),
obscureText: true,
style: style,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "API Key",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
_read() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return String
String stringValue = prefs.getString('apiKey') ?? '';
return stringValue;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
package:flutter/src/material/text_form_field.dart: failed assertion. initial value == null || controller == null is not true
我不确定我被困在哪里。我确实看到控制器正在初始化。我相信我的 _read() 方法有问题,但它确实返回一个字符串。
| 归档时间: |
|
| 查看次数: |
11596 次 |
| 最近记录: |