• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

Widget启动时读取设置

Flutter cps12345 12个月前 (12-11) 238次浏览 0个评论
问题:

Flutter: How to read preferences at Widget startup?

Widget启动时如何读取偏好设置?

解决方法:

使用 SharedPreferences

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController _controller;
  String _name;

  Future<Null> getSharedPrefs() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    _name = prefs.getString("name");
    setState(() {
      _controller = new TextEditingController(text: _name);
    });
  }

  @override
  void initState() {
    super.initState();
    _name = "";
    getSharedPrefs();  
  }

  @override
  Widget build(BuildContext context) {

    return new TextField(
                 decoration: new InputDecoration(
                   hintText: "Name (optional)",
                 ),
                 onChanged: (String str) {
                   setState(() {
                     _name = str;
                     storeName(str);
                 });
               },
               controller: _controller,
    );
  }
}
喜欢 (0)

您必须 登录 才能发表评论!