问题描述
这是我的代码
它一直显示加载程序,并且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)),child: Text(snapShot.data.email,child: Text(snapShot.data.phoneNo ?? "",)
],);
} else {
return SizedBox(
height: 30.0,width: 30.0,child: CircularProgressIndicator(
strokeWidth: 2.0,));
}
},)),);
} }
解决方法
为setState
分配值后,您需要添加userResponseFuture
;
helper.getString(TOKEN_ID).then((value) {
userResponseFuture = repos.getUserDetails(value);
setState((){})
});
因为它在build
方法之后,并且您的小部件无法调用正确的Future函数。