firebase - setState 允许更改立即出现在界面上以进行颤动
问题描述
我正在努力更新我在 firebase 中的用户名,我希望更改立即出现在界面上。我如何为此设置状态?这是迄今为止我为“更新”按钮编写的代码。谢谢!
RaisedButton(
onPressed: () async { //change username
FirebaseUser user = await Provider.of(context).auth.getCurrentUser();
UserUpdateInfo updateInfo = UserUpdateInfo();
updateInfo.displayName = _newUsernameController.text;
user.updateProfile(updateInfo);
print('USERNAME IS: ${user.displayName}');
setState(() {
//user.displayName = _newUsernameController;
});
Navigator.pop(context);
},
color: Colors.indigo,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50)),
child: Padding(
padding: EdgeInsets.only(top: 10, bottom: 10, left: 20, right: 20),
child: Text("Save", style: TextStyle(color: Colors.white,
fontWeight: FontWeight.bold, fontSize: 15),
),
),
)
解决方案
您可以分配user.displayName
给其中的变量setState
并显示该变量。
String username = 'Old Name';
...
child: Column(
children: <Widget>[
Text(username), // HERE!
RaisedButton(
onPressed: () async { //change username
FirebaseUser user = await Provider.of(context).auth.getCurrentUser();
UserUpdateInfo updateInfo = UserUpdateInfo();
updateInfo.displayName = _newUsernameController.text;
user.updateProfile(updateInfo);
print('USERNAME IS: ${user.displayName}');
setState(() {
username = user.displayName;
});
// Navigator.pop(context); why???
},
color: Colors.indigo,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50)),
child: Padding(
padding: EdgeInsets.only(top: 10, bottom: 10, left: 20, right: 20),
child: Text("Save", style: TextStyle(color: Colors.white,
fontWeight: FontWeight.bold, fontSize: 15),
),
),
),
],
),
推荐阅读
- python-3.x - 替换python数据框中的重复条目并根据其他列保留第一个值
- angular - 无法在我的 NX 工作区中使用 @angular-service-worker
- javascript - Route.post() 需要一个回调函数,但得到一个 [object String]
- plsql - 在plsql中将数据类型CHAR转换为数据类型boolean
- android - 自定义范围内没有出现 - 织物
- java - ARCore:垂直平面上的随机对象旋转
- c++ - 在 C++ 中使用 OpenCV 在斜线上方创建一个旋转矩形
- java - 多次使用不同的配置进行测试
- angular5 - 在角度 5 中返回三元运算符内的模板插值
- javascript - 到空间没有在 javascript 中被转换