【问题标题】:Future builder not updating once i have resposne一旦我有回应,Futurebuilder 不会更新
【发布时间】:2020-09-26 20:29:43
【问题描述】:

我有一个页面,一旦我得到响应,我需要显示项目

这是我的代码

它一直显示加载器,futureBuilder 永远不会被触发

@override
void initState() {
super.initState();
helper.getString(TOKEN_ID).then((value) {
  userResponseFuture = repos.getUserDetails(value);
  });
}

@override
Widget build(BuildContext context) {
return Scaffold(
  body: Container(
      margin: EdgeInsets.all(10),
      alignment: Alignment.center,
      color: Colors.white,
      child: FutureBuilder<UserResponse>(
        future: userResponseFuture,
        builder: (context, snapShot) {
          if (snapShot.hasData) {
            return Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Column(
                    children: [
                      CircleAvatar(
                        radius: 50.0,
                        backgroundImage: NetworkImage(snapShot
                                .data.profilePictureURL ??
                            "https://cdn.mos.cms.futurecdn.net/5e7ehPZf5RT6Jt2H9cQP6k-1024-80.jpg.webp"),
                        backgroundColor: Colors.brown,
                      ),
                      FlatButton(
                        child: Row(
                          children: [
                            Icon(Icons.edit),
                            Text("Edit profile Image"),
                          ],
                        ),
                        onPressed: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>
                                      ProfilePicScreen()));
                        },
                      ),
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                      snapShot.data.firstName +
                          " " +
                          snapShot.data.lastName,
                      style: TextStyle(color: Colors.blue.shade900)),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(snapShot.data.email,
                      style: TextStyle(color: Colors.blue.shade900)),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(snapShot.data.phoneNo ?? "",
                      style: TextStyle(color: Colors.blue.shade900)),
                )
              ],
            );
          } else {
            return SizedBox(
                height: 30.0,
                width: 30.0,
                child: CircularProgressIndicator(
                  strokeWidth: 2.0,
                ));
          }
        },
      )),
);

} }

【问题讨论】:

    标签: flutter flutter-futurebuilder


    【解决方案1】:

    userResponseFuture赋值后需要加上setState

    helper.getString(TOKEN_ID).then((value) {
      userResponseFuture = repos.getUserDetails(value);
      setState((){})
    });
    

    因为它在build 方法之后,并且您的小部件无法调用正确的未来函数。

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 2020-01-02
      相关资源
      最近更新 更多