【问题标题】:setState to allow change to appear on interface immediately for fluttersetState 允许更改立即出现在界面上以进行颤动
【发布时间】:2020-06-17 17:10:01
【问题描述】:

我正在努力更新我在 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),
                    ),
                  ),
                )

【问题讨论】:

    标签: firebase flutter settings setstate


    【解决方案1】:

    您可以将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),
                        ),
                      ),
                    ),
        ],
    ),
    

    【讨论】:

      【解决方案2】:

      如果您需要从多个小部件/视图访问数据,则应考虑搜索状态管理解决方案。

      看看来自 Flutter Docs 的 this documentation page,它对理解状态管理的基础知识很有帮助。

      user.updateProfile(updateInfo) 也是一个asynchronous task。您可能需要等待它,然后再采取进一步的操作,例如返回。

      【讨论】:

        猜你喜欢
        • 2020-06-30
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        • 1970-01-01
        • 1970-01-01
        • 2021-09-10
        • 2021-08-18
        相关资源
        最近更新 更多