【发布时间】:2021-11-25 01:54:31
【问题描述】:
四处寻找,但找不到解决方案。在我的应用程序中,我调用 FutureBuilder 从 firebase 加载文档列表。虽然它有效,但我得到了setState() or markNeedsBuild() called during build.。你们对我如何摆脱这些错误有任何想法吗?
这是我的功能:
Future<void> loadFollowers(List profilesId) async {
loading = true;
profilesId.forEach((id) async {
final DocumentSnapshot doc = await userRef.doc(id).get();
profile = Profile.fromDocument(doc);
followersList.add(profile);
});
loading = false;
notifyListeners();
}
以及我在哪里得到错误:
@override
Widget build(BuildContext context) {
return Stack(
children: [
gradientBackground(),
Scaffold(
appBar: AppBar(
brightness: Brightness.dark,
elevation: 0,
backgroundColor: Colors.transparent,
centerTitle: true,
title: Text('Seguidores',
style:
TextStyle(color: Colors.white, fontFamily: 'MontSerrat')),
),
backgroundColor: Colors.transparent,
body: FutureBuilder(
future: Provider.of<ProfileManager>(context, listen: false)
.loadFollowers(widget.followersId),
builder: (context, snapshot) {
return Consumer<ProfileManager>(
builder: (_, profileManager, __) {
if (profileManager.loading)
return circularProgress(Colors.white);
else
return ListView.builder(
itemCount: followersList.length,
itemBuilder: (context, index) {
//print(followersList[index].name);
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ProfileScreen(
/*posts: postManager
.getPosts(profiles[index].id),*/
home: false)));
},
child: Card(
margin: EdgeInsets.symmetric(
horizontal: 16, vertical: 8),
child: ListTile(
leading: CircleAvatar(
backgroundImage: CachedNetworkImageProvider(
profileManager
.followersList[index].picture ??
''),
),
title: Text(
profileManager.followersList[index].name ??
'',
style: TextStyle(
fontFamily: 'BebasNeue', fontSize: 20),
),
),
),
);
},
);
},
);
},
))
],
);
}
【问题讨论】: