Flutter-在每个应用重新启动后保留变量的值 [英] Flutter - Keeping variable's value after each app restart

查看:113
本文介绍了Flutter-在每个应用重新启动后保留变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一页上,我希望用户从一个变量( codeDialog )的默认文本开始,然后我希望他们更改该文本,在此之后,他们所写的文本将是我的新的默认文字.可悲的是我无法正常工作.

On my one page I would like my user to start with default text on one variable ( codeDialog) and then I want them to change that text, after that time that text they wrote would be my new default text. Sadly I couldn't get it to work.

现在,当我重新启动应用程序并打开该屏幕时,它将重置为 null .我认为这是因为初始化变量(为null),但我找不到解决该问题的方法.任何想法?谢谢.

Right now when I restart the app and open that screen it resets to null. I think its because of initializing variable ( which is null) but I couldn't find a way to fix that. Any idea? Thanks.

所有这些都在statefulwidget的状态类中:

All of this is inside statefulwidget's state class:

void initState() {
    super.initState();
    print(codeDialog);
    isFirstTime().then((isFirstTime) {
      isFirstTime ? defaultText() : readFromShared();
    });
    setText();
    print(codeDialog);
    _textEditingController = new TextEditingController(text: codeDialog);
  }
Future<bool> isFirstTime() async {
    final prefs = await SharedPreferences.getInstance();
    var isFirstTime = prefs.getBool('first_time');
    if (isFirstTime != null && !isFirstTime) {
      prefs.setBool('first_time', false);
      return false;
    } else {
      prefs.setBool('first_time', false);
      return true;
    }
  }
  static String codeDialog;
  void defaultText(){
    print("first time");
    codeDialog="This is what i want my default variable to be when my app first opened.";
  }

  void readFromShared() async{
    print("not first");
    final prefs = await SharedPreferences.getInstance();
    codeDialog = prefs.getString('sharedmessage');
  }
  void setText() async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setString('sharedmessage', codeDialog);
  }

然后我有了AlertDialog,它可以工作并保存数据.

and then i have AlertDialog with this it works and saves the data.

AlertDialog(
            scrollable: true,
            contentPadding: EdgeInsets.fromLTRB(20, 20, 20, 0),
            content: Column(
              children: [
                Container(
                  height: maxLines * 10.0,
                  child: TextField(
                    controller:  new TextEditingController(text: codeDialog),//_textEditingController,
                    maxLines: maxLines,
                    onChanged: (value){
                      setState(() {
                        valueText=value;
                      });
                    },
                    decoration: InputDecoration(
                      hintText: "Text here",
                    ),
                  ),
                ),
              ],
            ),
            actions: [
              TextButton(
                  onPressed: Navigator.of(context).pop, child: Text("Cancel")),
              TextButton(
                  onPressed: () {
                    setState(() {
                      Navigator.pop(context);
                      codeDialog = valueText;
                      print(codeDialog);
                      setText();
                    });
                  },
                  child: Text("Save"))
            ],
          )

也值得一提的是可能看到此问题的人.如果您不更改TextField的值并提交(由于某种原因在应用重启后),则该值将更改为null(我认为由于字符串没有变化).您可以通过添加 if(value == null)并将其保存(如果其不为null)来进行修复.

Also worth noting to people who may see this question. If you don't change TextField's value and submit it (After app restart for some reason) the value will change to null (i think due to there's no change in string). You can fix it by adding if(value==null) and saving it if its not null.

推荐答案

您必须使用软件包共享的首选项.我将使用源代码中的示例进行编辑

You have to use the package shared preference. I will edit with an example from my source codes

我会使用FutureBuilder而不是initstate,因为它更干净:

I would use FutureBuilder instead of initstate as it is cleaner:

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SavedPreference extends StatefulWidget {
  SavedPreference({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _SavedPreference createState() => _SavedPreference();
}

class _SavedPreference extends State<SavedPreference> {
  TextEditingController _textEditingController;
  String valueText;

  void initState() {
    super.initState();
  }

  Future<String> _getSharedValue() async {
    const defaultText =
        "This is what i want my default variable to be when my app first opened.";
    String ret;
    var prefs = await SharedPreferences.getInstance();
    bool isFirstTime = prefs.getBool('firstTime');
    print(isFirstTime);

    ret =
        (isFirstTime == null || isFirstTime == true) ? defaultText : prefs.getString('sharedmessage');
    print(ret);

    _textEditingController = new TextEditingController(text: ret);
    return ret;
  }

  String codeDialog;

  void setText() async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setString('sharedmessage', valueText);
    prefs.setBool('firstTime', false);
  }

  @override
  Widget build(BuildContext context) {
    int maxLines = 10;
    return Container(
        child: FutureBuilder(
            future: _getSharedValue(),
            builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
              print(snapshot.data);
              if (snapshot.hasData) {
                return TextButton(
                    child: Text(snapshot.data),
                    onPressed: () async => await showDialog(
                        context: context,
                        builder: (context) => new AlertDialog(
                              scrollable: true,
                              contentPadding:
                                  EdgeInsets.fromLTRB(20, 20, 20, 0),
                              content: Column(
                                children: [
                                  Container(
                                    height: maxLines * 10.0,
                                    child: TextField(
                                      controller: new TextEditingController(
                                          text: codeDialog),
                                      maxLines: maxLines,
                                      onChanged: (value) {
                                        valueText = value;
                                      },
                                      decoration: InputDecoration(
                                        hintText: "Text here",
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              actions: [
                                TextButton(
                                    onPressed: Navigator.of(context).pop,
                                    child: Text("Cancel")),
                                TextButton(
                                    onPressed: () {
                                      setState(() {
                                        Navigator.pop(context);
                                        codeDialog = valueText;
                                        print(codeDialog);
                                        setText();
                                      });
                                    },
                                    child: Text("Save"))
                              ],
                            )));
              } else
                return CircularProgressIndicator();
            }));
  }
}

您的问题是initState未正确等待 await SharedPreferences.getInstance(); 的结果(我试图将其修复在initState中,并浪费了大量时间)

Your problem was that initState was not awaiting properly the result of await SharedPreferences.getInstance(); (I tried to fix it inside initState and lost a tremendous amount of time)

这篇关于Flutter-在每个应用重新启动后保留变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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