Flutter:如何在Widget启动时读取首选项? [英] Flutter: How to read preferences at Widget startup?

查看:163
本文介绍了Flutter:如何在Widget启动时读取首选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在Widget启动时读取首选项,但一直找不到解决方案。
我希望在TextField中显示用户名(他们可以更改),并将其存储在首选项中,以便用户返回页面后立即显示。

I have been trying to read preferences at Widget startup but have been unable to find a solution. I wish to show the users name in a TextField (which they can change) and store it in preferences so that it is shown as soon as they go back to the page.

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,
             )
  }
}

我从以下位置了解了在initState()上使用异步的想法:

API调用后有状态小部件的颤动时序问题

但是异步似乎在启动时导致此错误:

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的示例,但似乎找不到与我尝试的相似的示例

I looked for examples of FutureBuilder but cannot seem to find any which are similar to what I am trying to do.

推荐答案

我建议不要在initState()上使用异步。但是您可以通过将您的SharedPreferences包装在另一个函数中并声明为异步来以不同的方式进行操作。

I would suggest not to use the async on initState(). but you can do this in a different way by wrapping up your SharedPreferences inside another function and declaring this as async.

我已经修改了您的代码。请检查是否可行。

I have modified your code . Please check if this works. Many Thanks.

修改后的代码:

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,
    );
  }
}

让我知道是否有帮助。
谢谢。

Let me know if this helps. Thank you.

这篇关于Flutter:如何在Widget启动时读取首选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆