Firebase 将数据从firestore加载到带有颤振的列表时出现问题
Firestore文档包含特定的用户信息(用户名、年龄、挑战等)。当然,一个用户可能面临多个挑战。我正在将特定的用户数据加载到对象列表中,但当我将该列表返回给FutureBuilder时,问题就出现了。假设我有2个用户,每个用户都有3个挑战。当我返回列表时,它显示正确的长度(6),但只包含加载到列表中的最后一个用户的数据。My ListView.builder显示6个卡片生成器,但这些卡片中的数据相同 我试图用Map列表替换构造函数列表,但结果是一样的。这可能是个逻辑错误,但我找不到Firebase 将数据从firestore加载到带有颤振的列表时出现问题,firebase,dart,flutter,google-cloud-firestore,Firebase,Dart,Flutter,Google Cloud Firestore,Firestore文档包含特定的用户信息(用户名、年龄、挑战等)。当然,一个用户可能面临多个挑战。我正在将特定的用户数据加载到对象列表中,但当我将该列表返回给FutureBuilder时,问题就出现了。假设我有2个用户,每个用户都有3个挑战。当我返回列表时,它显示正确的长度(6),但只包含加载到列表中的最后一个用户的数据。My ListView.builder显示6个卡片生成器,但这些卡片中的数据相同 我试图用Map列表替换构造函数列表,但结果是一样的。这可能是个逻辑错误,但我找不到 Futur
Future<List<AllChallengesStorage>> challengesToFeed () async {
var queryResult = await Firestore.instance.collection("Users")
.where("total_challenges", isGreaterThanOrEqualTo: 1)
.getDocuments();
if (queryResult.documents.isNotEmpty){
for (int i=0; i < queryResult.documents.length; ++i){
for (int j=0; j < queryResult.documents[i].data['total_challenges']; ++j){
widget.challengesInfo.username = queryResult.documents[i].data['username'];
widget.challengesInfo.name = queryResult.documents[i].data['challenges'][j]['name'];
widget.challengesInfo.description = queryResult.documents[i].data['challenges'][j]['description'];
widget.challengesInfo.duration = queryResult.documents[i].data['challenges'][j]['duration'];
allChallenges.add(widget.challengesInfo);
}
}
}
return allChallenges;
}
class _FeedState extends State<Feed> {
List<AllChallengesStorage> allChallenges = [];
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<List<AllChallengesStorage>>(
future: challengesToFeed(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) return Center(child: Text("Loading..."));
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return _cardBuilder(context, snapshot, index);
},
);
},
),
);
}
Future challengesToFeed()异步{
var queryResult=await Firestore.instance.collection(“用户”)
其中(“总挑战”大于或等于1)
.getDocuments();
if(queryResult.documents.isNotEmpty){
对于(int i=0;i
问题在于,您使用的是同一个对象,只是在每个循环上覆盖了它的属性。您需要的是为每个新卡实例化一个新对象,比如在每次完成第二个循环时使用模型类创建一个新的帮助器对象
for (int i=0; i < queryResult.documents.length; ++i){
ClassName helper = ClassName();
for (int j=0; j < queryResult.documents[i].data['total_challenges']; ++j){
helper.username = queryResult.documents[i].data['username'];
helper.name = queryResult.documents[i].data['challenges'][j]['name'];
helper.description = queryResult.documents[i].data['challenges'][j]['description'];
helper.duration = queryResult.documents[i].data['challenges'][j]['duration'];
allChallenges.add(helper);
}
}
}
问题是,您使用的是同一个对象,只是在每个循环上覆盖了它的属性。您需要的是为每个新卡实例化一个新对象,比如在每次完成第二个循环时使用模型类创建一个新的帮助器对象
for (int i=0; i < queryResult.documents.length; ++i){
ClassName helper = ClassName();
for (int j=0; j < queryResult.documents[i].data['total_challenges']; ++j){
helper.username = queryResult.documents[i].data['username'];
helper.name = queryResult.documents[i].data['challenges'][j]['name'];
helper.description = queryResult.documents[i].data['challenges'][j]['description'];
helper.duration = queryResult.documents[i].data['challenges'][j]['duration'];
allChallenges.add(helper);
}
}
}