期望的标识符.用于GroupModel.fromDocumentSnapshot flutter &



这是预期的标识符。在late final groupId2 = GroupModel.fromDocumentSnapshot(doc: );

我不知道该在里面放什么。我是一个新手

屏幕:

late final groupId2 = GroupModel.fromDocumentSnapshot(doc: //Expected an identifier HERE);

这是组模型:

factory GroupModel.fromDocumentSnapshot({required DocumentSnapshot doc}) {
return GroupModel(
id: doc.id,
name: (doc.data() as Map<String, dynamic>)["name"],
leader: (doc.data() as Map<String, dynamic>)["leader"],
members: List<String>.from((doc.data() as Map<String, dynamic>)["members"]),
groupCreate: (doc.data() as Map<String, dynamic>)["groupCreate"],
);
}

这是我之前创建的模型。我希望这对你有帮助。

class ProductModel {
static const ID = "id";
static const IMAGE = "image";
static const NAME = "name";
static const BRAND = "brand";
static const PRICE = "price";
String? id;
String? image;
String? name;
String? brand;
int? price;
ProductModel(
{required this.id,
required this.image,
required this.name,
required this.brand,
required this.price});
factory ProductModel.fromFire(QueryDocumentSnapshot snapshot) {
return ProductModel.fromMap(snapshot as Map<String, dynamic>);
}
factory ProductModel.fromDocumentSnapshot({required DocumentSnapshot<Map<String,dynamic>> doc}){
// print(doc.data()!["id"]);
// print(doc.data()!["image"]);
// print(doc.data()!["name"]);
// print(doc.data()!["brand"]);
// print(doc.data()!["price"]);
return ProductModel(
id: doc.data()!["id"],
image: doc.data()!["image"],
name: doc.data()!["name"],
brand: doc.data()!["brand"],
price: doc.data()!["price"],
);
}
Map<String, dynamic> toJson() => {
'id': id,
'image': image,
'name': name,
'brand': brand,
'price': price,
};
ProductModel.fromMap(Map<String, dynamic> data) {
id = data[ID];
image = data[IMAGE];
name = data[NAME];
brand = data[BRAND];
price = data[PRICE];
}
}

源代码:定义函数

Future<void> getMessagesTestTest() async{
var aa = await _firestore.collection('ProductModel').snapshots().map((notes){
notesFromFirestore.clear();
for(final DocumentSnapshot<Map<String,dynamic>> doc in notes.docs){
log("start in loop");
log('doc.id == ${doc.id}');
// print("start in loop");
// print('doc.id == ${doc.id}');
notesFromFirestore.add(ProductModel.fromDocumentSnapshot(doc:doc));
// print(ProductModel.fromDocumentSnapshot(doc:doc));
}
// print(notesFromFirestore.first.id.toString());
return notesFromFirestore;});
// int count = await allData.length;
aa.listen(
(event) => print("Data Retrieved"),
);
// print(aa.length);
print( 'List Size = ${notesFromFirestore.length}');
var count = 0;
for (final ProductModel notesFromFirestoreA in notesFromFirestore) {
count++;
print('Loop Counter == ${count}');
print('Id == ${notesFromFirestoreA.id}');
print('Name == ${notesFromFirestoreA.name}');
print('Price == ${notesFromFirestoreA.price}');
print('Brand == ${notesFromFirestoreA.brand}');
print('image == ${notesFromFirestoreA.image}');
}
}

相关内容

  • 没有找到相关文章

最新更新