flutter - 如何在颤振中保存变量并使其可供我的所有文件访问?
问题描述
我基本上从用户那里得到了一个密码和 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;
}
}
解决方案
这很简单,在你所有的类/小部件/函数之外你创建变量就像
var sharedPass;
并在需要时更改它的值,例如
Container(
child: RaisedButton(
child: Text("Submit"),
onPressed: () async {
sharedPass = controller.text;
currentCollection.createCollection(controller.text, code);
},
),
),
现在你可以在任何地方使用它的价值,比如
Text(sharedPass)
推荐阅读
- typescript - 获取泛型对象中已知属性的类型
- swift - 如何在 NSStatusBar 中绘制自定义视图?
- c++ - TSharedPtr 全局变量
- javascript - 通过索引从 Jquery 选择器中获取元素
- javascript - 如何计算一年中 2 个日期和周之间的天数(javascript)
- vue.js - Vuetify touch 支持滑动元素与动画效果(可拖动)
- css - Angular 8 Material - 自定义选择选项样式以满足您的需求
- web - 更改服务器后,我网站上的老用户无法再访问它
- java - 关于 DateTimeZone.forID 的两个不同结果
- javascript - Javascript 弹出框(警报、确认和提示)是“文档”的一部分还是“浏览器”的一部分(客户端)