我在制作我的移动应用程序使用扑动。我想显示特定的数据从我的数据库在firebase到我的应用程序。问题是,当我运行hasData输出说它没有数据,但我已经有一些数据输入在我的集合在firestore。
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('customers')
.snapshots(),
builder: (
BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot,
) {
if (snapshot.hasData) {
var data = 'No Data';
return Text(data);
}
if (snapshot.connectionState == ConnectionState.waiting) {
var data = 'Loading';
return Text(data);
}
final data = snapshot.requireData;
return ListView.builder(
itemCount: data.size,
itemBuilder: (context, index) {
return Container(
child: Column(
children: [
detailsWidget(
icon: Icons.person,
field: 'Name',
value: '${data.docs[index]['name']}',
),
detailsWidget(
icon: Icons.order,
field: 'Order',
value: '${data.docs[index]['order']}',
),
],
),
);
});
}),
我在一个单独的类
中初始化了变量import 'package:cloud_firestore/cloud_firestore.dart';
class DatabaseManagement {
final CollectionReference customerList =
FirebaseFirestore.instance.collection('customers');
Future<void> createUserData(
String name,
String order,
String uid,
) async {
return await customerList.doc(uid).set({
'name': name,
'order': order
});
}
}
现在我不知道如何改进我的代码,使输出显示
试试这段代码
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('customers')
.snapshots(),
builder: (
BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot,
) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
var data = 'Loading';
return Text(data);
case ConnectionState.active:
case ConnectionState.done:
if (snapshot.hasData) {
var data = 'No Data';
return Text(data);
} else {
final data = snapshot.requireData;
return ListView.builder(
itemCount: data.size,
itemBuilder: (context, index) {
return Container(
child: Column(
children: [
detailsWidget(
icon: Icons.person,
field: 'Name',
value: '${data.docs[index]['name']}',
),
detailsWidget(
icon: Icons.oder,
field: 'Order',
value: '${data.docs[index]['order']}',
),
],
),
);
});
}
default:
return const Center(
child: Text("Somethign went wrong"),
);
}
}),