Future builder not updating once i have resposne

74 views Asked by At

I have a page where once i have response i need to display items

here is my code

It keeps on showing loader and futureBuilder never gets triggered

@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,
                ));
          }
        },
      )),
);

} }

1

There are 1 answers

0
Ali Bayram On BEST ANSWER

You need to add setState after assign a value to userResponseFuture;

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

because it is being after build method and your widget can't call the right future function.