首页 > 解决方案 > 如何在颤振中保存变量并使其可供我的所有文件访问?

问题描述

我基本上从用户那里得到了一个密码和 6 位代码,我想把它保存到一个变量中。我知道它已经保存为“controller.text”和“code”,但是一旦我进入下一个屏幕,我将无法访问它们。我尝试使用一个类并在那里传递这两个参数,但这意味着我每次想要获取密码/代码时都必须实例化它,我将无法这样做,因为我没有任何东西可以通过。有人可以帮助或建议一种不同的方式吗?

Container(
            child: RaisedButton(
              child: Text("Submit"),
              onPressed: () async {
                 currentCollection.createCollection(controller.text, code);
                },
            ),
          ),
class Collection{

  static  String password;
  static  int code;
  Collection(password,code);
  static final  String collectionName = password;
  static final int passcode = code;

  getPassword()
  {
  return password;
  }
  getCode()
  {
  return code;
  }



}

标签: flutterdart

解决方案


这很简单,在你所有的类/小部件/函数之外你创建变量就像

var sharedPass;

并在需要时更改它的值,例如

Container(
            child: RaisedButton(
              child: Text("Submit"),
              onPressed: () async {
                 sharedPass = controller.text;
                 currentCollection.createCollection(controller.text, code);
                },
            ),
          ),

现在你可以在任何地方使用它的价值,比如

Text(sharedPass)

推荐阅读