【发布时间】:2020-06-14 04:07:50
【问题描述】:
如果在我的文本表单字段上创建了一个初始值,我正在尝试设置一个初始值。当我运行我的代码时:
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;
}
我收到以下错误:
package:flutter/src/material/text_form_field.dart: failed assertion. initial value == null || controller == null is not true
我不确定自己被困在哪里。我确实看到控制器正在初始化。我相信我的 _read() 方法有问题,但它确实返回了一个字符串。
【问题讨论】:
-
用 FutureBuilder 包装并提供初始值为 ""