我一直在try 阅读Widget创业公司的首选项,但一直无法找到解决方案. 我希望在TextField(他们可以更改)中显示用户名,并将其存储在Preferences中,以便在他们返回页面时立即显示.
class _MyHomePageState extends State<MyHomePage> {
TextEditingController _controller;
:
:
Future<Null> storeName(String name) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("name", name);
}
@override
initState() async {
super.initState();
SharedPreferences prefs = await SharedPreferences.getInstance();
_controller = new TextEditingController(text: prefs.getString("name"));
}
@override
Widget build(BuildContext context) {
:
:
return new TextField(
decoration: new InputDecoration(
hintText: "Name (optional)",
),
onChanged: (String str) {
setState(() {
_name = str;
storeName(str);
});
},
controller: _controller,
)
}
}
I got the idea for using async on initState() from :
flutter timing problems on stateful widget after API call
But the async seems to cause this error on startup :
'package:flutter/src/widgets/framework.dart': Failed assertion: line 967 pos 12:
'_debugLifecycleState == _StateLifecycle.created': is not true.
我寻找了FutureBuilder的示例,但似乎找不到任何与我正在try 做的事情相似的示例.