我正在尝试使用以下代码显示来自 firestore 的数据列表:
@override
Widget build(BuildContext context) {
// <1> Use FutureBuilder
return FutureBuilder<QuerySnapshot>(
// <2> Pass `Future<QuerySnapshot>` to future
future: FirebaseFirestore.instance.collection('UserNames').get(),
builder: (context, snapshot) {
if (snapshot.hasData) {
// <3> Retrieve `List<DocumentSnapshot>` from snapshot
final List<DocumentSnapshot> documents = snapshot.data.docs;
return ListView(
children: documents
.map((doc) => Card(
child: ListTile(
title: Text(doc['displayName']),
subtitle: Text(doc['plastics'].toString()),
),
))
.toList());
} else if (snapshot.hasError) {
return Text('Its Error!');
}
});
}
这段代码在一个名为 Scoreboard() 的类中,我试图从另一个名为 createdGroupHomePage() 的类中调用该类,如下所示:
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Palette.lightGreen,
title: const Text('Group Home'),
leading: GestureDetector(
onTap: () {
Navigator.of(context)
.pushAndRemoveUntil(HomeScreen.route, (route) => false);
},
child: Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
),
),
body: Builder(
builder: (context) => Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Align(
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 50,
),
FutureBuilder(
future: getGroupName(),
builder: (_, AsyncSnapshot snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Palette.lightGreen,
));
}
return Text(snapshot.data,
style: TextStyle(
color: Palette.lightGreen, fontSize: 30));
},
),
SizedBox(height: 20),
Scoreboard(), // Right here I am trying to display the list contents
],
),
),
],
),
),
),
);
}
不幸的是,当我转到应用程序中的页面时,它显示像素溢出但有 9,000 的东西,我真的不知道该怎么办。我是否必须将 Scoreboard() 包装在某些东西中,还是其他东西?