Closed. This question needs
details or clarity。它当前不接受答案。
想改善这个问题吗?添加详细信息,并通过
editing this post阐明问题。
2个月前关闭。
Improve this question
我想将数据作为List存储到我的Model类中,这是我的Model类代码
class BookingList {
String bookingId;
String userId;
String vendorId;
String mealName;
String vendorName;
String timestamp;
bool paymentStatus;
int mealPrice;
BookingList(this.bookingId, this.userId, this.vendorId, this.mealName,
this.vendorName, this.timestamp, this.paymentStatus, this.mealPrice);
}
Firebase数据库如下所示
谁能帮我?
提前致谢...
首先,您必须使用fromJson函数创建模型类,该函数将json转换为模型类。我为您准备了类(class)。您也可以从这里https://javiercbk.github.io/json_to_dart/
class BookingList {
String bookingId;
String userId;
String vendorId;
String mealName;
String timestamp;
bool paymentStatus;
int mealPrice;
BookingList(
{this.bookingId,
this.userId,
this.vendorId,
this.mealName,
this.timestamp,
this.paymentStatus,
this.mealPrice});
BookingList.fromJson(Map<String, dynamic> json) {
bookingId = json['bookingId'];
userId = json['userId'];
vendorId = json['vendorId'];
mealName = json['mealName'];
timestamp = json['timestamp'];
paymentStatus = json['paymentStatus'];
mealPrice = json['mealPrice'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['bookingId'] = this.bookingId;
data['userId'] = this.userId;
data['vendorId'] = this.vendorId;
data['mealName'] = this.mealName;
data['timestamp'] = this.timestamp;
data['paymentStatus'] = this.paymentStatus;
data['mealPrice'] = this.mealPrice;
return data;
}
}
然后,您必须解析Firebase中的数据。
Future<List<BookingList>> getAllData() async {
print("Active Users");
var val = await fireStore
.collection("booking")
.getDocuments();
var documents = val.documents;
print("Documents ${documents.length}");
if (documents.length > 0) {
try {
print("Active ${documents.length}");
return documents.map((document) {
BookingList bookingList = BookingList.fromJson(Map<String, dynamic>.from(document.data));
return bookingList;
}).toList();
} catch (e) {
print("Exception $e");
return [];
}
}
return [];
}
我是一名优秀的程序员,十分优秀!