我尝试从名为“ products”的Firestore集合中获取所有带有其子集合“ userProducts”的文档。在运行控制台中没有返回错误,但是我没有从数据库中获取任何数据。您能看一下我的代码并更正我的错误吗?
这是我要显示Firestore数据的页面:
class Shop extends StatefulWidget {
@override
_ShopState createState() => _ShopState();
}
class _ShopState extends State<Shop> {
String postOrientation = "grid";
String shopOrientation = "grid";
bool isFollowing = false;
bool isLoading = false;
List<Prod> finalProductsList = [] ;
productsList() async {
List list_of_products = await Firestore.instance.collection("products")
.getDocuments()
.then((val) => val.documents);
for (int i=0; i<list_of_products.length; i++) {
Firestore.instance.collection("products").document(
list_of_products[i].documentID.toString()).collection("userProducts").snapshots().listen(CreateListofProducts);
}
}
CreateListofProducts(QuerySnapshot snapshot) async {
var docs = snapshot.documents;
for (var Doc in docs) {
finalProductsList.add(Prod.fromFireStore(Doc));
}
}
buildProducts() {
if (isLoading) {
return circularProgress();
} else if (finalProductsList.isEmpty) {
return Container(
color: kSecondaryColor,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 20.0),
child: Text(
"No Products",
style: TextStyle(
color: Colors.redAccent,
fontSize: 40.0,
fontWeight: FontWeight.bold,
),
),
),
],
),
);
} else {
return finalProductsList.toList();
}
}
@override
Widget build(BuildContext context) {
return
Scaffold(
appBar: AppBar(backgroundColor: kPrimaryColor,
title: Text('Shop',
style: TextStyle(
fontFamily :"MajorMonoDisplay",
fontSize: 35.0 ,
color: Colors.white),),
iconTheme: IconThemeData(color: kSecondaryColor),
actions: <Widget>[
IconButton( icon: Icon(Icons.search), onPressed: () {
setState(() {
});
}
),],
),
backgroundColor: kSecondaryColor,
body:NeuomorphicContainer(
margin: EdgeInsets.only(top:10.0,left: 10.0,right: 10.0, bottom: 10.0 ),
width: 675,
height: 789,
color: Color(0xFFFeFeFe),
borderRadius: BorderRadius.circular(20.0),
intensity: 0.260,
offset: Offset(5.0,5.0),blur: 3.0,
style: NeuomorphicStyle.Pressed,
child: buildProducts(),
),
);
}
}
这是声明自定义模型类的页面:
class Prod extends StatefulWidget {
final String prodId;
final String ownerId;
final String username;
final String price;
final String productname;
final String details;
final String color;
final String composition;
final String washandcare;
final String sizeandfit;
final String shopmediaUrl;
final dynamic likes;
Prod({
this.prodId,
this.ownerId,
this.username,
this.price,
this.details,
this.productname,
this.color,
this.composition,
this.washandcare,
this.sizeandfit,
this.shopmediaUrl,
this.likes,
// String id,
});
factory Prod.fromFireStore(DocumentSnapshot doc) {
return Prod(
prodId: doc['prodId'],
ownerId: doc['ownerId'],
username: doc['username'],
price: doc['price'],
productname: doc['productname'],
details: doc['details'],
shopmediaUrl: doc['shopmediaUrl'],
color:doc['color'],
composition:doc['composition'],
washandcare:doc['washandcare'],
sizeandfit:doc['sizeandfit'],
likes: doc['likes'],
);
}
int getLikeCount(likes) {
//if no likes return 0
if (likes == null) {
return 0;
}
int count = 0;
// if the key is explicitly set to true, add a like
likes.values.forEach((val) {
if (val == true) {
count += 1;
}
});
return count;
}
@override
_ProdState createState() => _ProdState(
prodId: this.prodId,
ownerId: this.ownerId,
username: this.username,
price: this.price,
details: this.details,
productname: this.productname,
color: this.color,
composition: this.composition,
washandcare: this.washandcare,
sizeandfit: this.sizeandfit,
shopmediaUrl: this.shopmediaUrl,
likes: this.likes,
likeCount: getLikeCount(this.likes),
);
}
class _ProdState extends State<Prod> {
final String currentUserId = currentUser?.id;
final String prodId;
final String ownerId;
final String username;
final String price;
final String productname;
final String details;
final String color;
final String composition;
final String washandcare;
final String sizeandfit;
final String shopmediaUrl;
int likeCount;
Map likes;
bool isLiked;
bool showHeart = false;
bool details1 = true;
_ProdState({
this.prodId,
this.ownerId,
this.username,
this.price,
this.details,
this.productname,
this.color,
this.composition,
this.washandcare,
this.sizeandfit,
this.shopmediaUrl,
this.likes,
this.likeCount,
});
buildPostHeader() {
return FutureBuilder(
future: usersRef.document(ownerId).get(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return circularProgress();
}
User user = User.fromDocument(snapshot.data);
bool isPostOwner = currentUserId == ownerId;
return NeuomorphicContainer(
margin: EdgeInsets.only(top:10.0,left: 10.0,right: 10.0, bottom: 10.0 ),
color: Color(0xFFFeFeFe),
borderRadius: BorderRadius.circular(20.0),
intensity: 0.260,
offset: Offset(5.0,5.0),blur: 3.0,
style: NeuomorphicStyle.Pressed,
child: Column(
children: <Widget> [
ListTile(
leading: CircleAvatar(
backgroundImage: CachedNetworkImageProvider(user.photoUrl),
backgroundColor: Colors.grey,
),
title: GestureDetector(
onTap: () => showProfile(context, profileId: user.id),
child: Text(
user.username,
style: TextStyle(
color: kText,
fontWeight: FontWeight.bold,
),
),
),
// subtitle: Text(location,
// style: TextStyle(color: kGrey),),
trailing: isPostOwner
? IconButton(
icon: Icon(Icons.more_vert,color: Colors.white,),
onPressed: () => handleDeletePost(context),
)
: Text(''),
),
GestureDetector(
onDoubleTap: handleLikePost,
child: Stack(
alignment: Alignment.center,
children: <Widget>[
cachedNetworkImage(shopmediaUrl),
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(padding: EdgeInsets.only(top: 40.0, left: 20.0)),
GestureDetector(
onTap: handleLikePost,
child: Icon(
isLiked ? Icons.favorite : Icons.favorite_border,
size: 28.0,
color: kIcon,
),
),
// Padding(padding: EdgeInsets.only(right: 1.0)),
Container(
// margin: EdgeInsets.only(left: 20.0),
child: Text(
"$likeCount likes",
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
// fontWeight: FontWeight.bold,
),
),
),
Padding(padding: EdgeInsets.only(right: 20.0)),
GestureDetector(
onTap: () => showProductComments(
context,
prodId: prodId,
ownerId: ownerId,
shopmediaUrl: shopmediaUrl,
),
child: Icon(
Icons.chat,
size: 28.0,
color: kIcon,
),
),
],
),
// Row(
// children: <Widget>[
//
// ],
// ),
SizedBox( height:10.0,),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
color: kPrimaryColor,
padding: EdgeInsets.only(bottom: 10.0),
margin: EdgeInsets.only(left: 20.0),
child: Text(
"$productname ",
style: TextStyle(
color: kText,
fontWeight: FontWeight.bold,
),
),
),
// SizedBox( height:10.0,),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[Container(
color: kPrimaryColor,
padding: EdgeInsets.only(bottom: 10.0),
margin: EdgeInsets.only(left: 20.0),
child: Text(
"₹$price ",
style: TextStyle(
color: kText,
fontWeight: FontWeight.bold,
),
),
),],
),
@override
Widget build(BuildContext context) {
isLiked = (likes[currentUserId] == true);
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
buildPostHeader(),
//
],
);
}
}
答案 0 :(得分:0)
我认为未调用productsList()。
尝试将其放入初始化状态。
答案 1 :(得分:0)
我认为您需要做
CreateListofProducts(QuerySnapshot snapshot)async
{
var docs = snapshot.documents;
for (var Doc in docs)
{
finalProductsList.add(Prod.fromFireStore(Doc.data)); //.data
}
}
然后在内部
class Prod extends StatefulWidget
factory Prod.fromFireStore(Map<String, dynamic> doc) {
return Prod(
prodId: doc['prodId'],
ownerId: doc['ownerId'],
username: doc['username'],
price: doc['price'],
productname: doc['productname'],
details: doc['details'],
shopmediaUrl: doc['shopmediaUrl'],
color:doc['color'],
composition:doc['composition'],
washandcare:doc['washandcare'],
sizeandfit:doc['sizeandfit'],
likes: doc['likes'],
);
}
也许还有别的东西,但是您应该纠正它