如何 fetch/retrieve 排列 Firestore 并使用 Getx 在 flutter 上显示
How to fetch/retrieve array Firestore and display on flutter using Getx
我的 collection 字段上有一家餐厅,例如这家餐厅有一个报价
'15% 折扣' 我已经在我的 flutter 应用程序上显示了这部分。
现在假设我有另一家餐厅提供多项优惠{'0': 'DISCOUNT 5%', '1': 'DISCOUNT 10%'}
我该如何在我的应用程序上显示它,我尝试了以下但没有用
Here is my collection offer field
这是代码:
class OfferModel {
String id;
List offer;
OfferModel({
this.id,
this.offer,
});
factory OfferModel.fromJson(Map<String, dynamic> json, elementId) =>
OfferModel(
id: elementId,
offer: json ['offer'],
);
Map<String, dynamic> toJson() => {
"offer": offer,
};
class OfferDetail extends StatelessWidget {
final OfferModel currentOffer;
OfferDetail(this.currentOffer);
final controller = Get.put(OfferDetailController());
@override
Widget build(BuildContext context) {
controller.offer = currentOffer;
Widget offerSection = Container(
child: Text(
currentOffer.offer,
);
return Scaffold(
body: Stack(
children: [
Column(
children: [
Expanded(
child: ListView(
children: [
offerSection,
Padding(
padding:
EdgeInsets.symmetric(vertical: 15, horizontal: 15),
child: Align(
child: ButtonFayda(
title: 'Get offer',
onPressed: () {
controller.offerId = currentOffer.id;
controller.claimOffer();
},
),
alignment: Alignment.bottomCenter,
),
),
],
),
),
],
)
],
),
);
}
}
class OfferDetailController extends GetxController {
var offer = OfferModel();
var offerId;
RxList<OfferModel> offerList = <OfferModel>[].obs;
var isLoading = true.obs;
//rest of the code
}
我无法理解你的代码,但我可以理解你的问题如下。
你有这样一个对象
Object{
field1 String,
field2 List<String>
}
在您的 Firestore 中,您拥有相应对象的数据。现在您想知道如何获取 field2 数组。
所以这可以使用 List.castFrom(data['field2']).
来实现
例如
QuerySnapshot<Map<String, dynamic>> data = await FirebaseFirestore.instance.collection('object').get();
List<Object> objList = data.docs.map<Object>((data) =>
new Shop(
field1: data['field1'],
field2: List.castFrom(data['field2'])
)
).toList();
我的 collection 字段上有一家餐厅,例如这家餐厅有一个报价 '15% 折扣' 我已经在我的 flutter 应用程序上显示了这部分。
现在假设我有另一家餐厅提供多项优惠{'0': 'DISCOUNT 5%', '1': 'DISCOUNT 10%'}
我该如何在我的应用程序上显示它,我尝试了以下但没有用
Here is my collection offer field
这是代码:
class OfferModel {
String id;
List offer;
OfferModel({
this.id,
this.offer,
});
factory OfferModel.fromJson(Map<String, dynamic> json, elementId) =>
OfferModel(
id: elementId,
offer: json ['offer'],
);
Map<String, dynamic> toJson() => {
"offer": offer,
};
class OfferDetail extends StatelessWidget {
final OfferModel currentOffer;
OfferDetail(this.currentOffer);
final controller = Get.put(OfferDetailController());
@override
Widget build(BuildContext context) {
controller.offer = currentOffer;
Widget offerSection = Container(
child: Text(
currentOffer.offer,
);
return Scaffold(
body: Stack(
children: [
Column(
children: [
Expanded(
child: ListView(
children: [
offerSection,
Padding(
padding:
EdgeInsets.symmetric(vertical: 15, horizontal: 15),
child: Align(
child: ButtonFayda(
title: 'Get offer',
onPressed: () {
controller.offerId = currentOffer.id;
controller.claimOffer();
},
),
alignment: Alignment.bottomCenter,
),
),
],
),
),
],
)
],
),
);
}
}
class OfferDetailController extends GetxController {
var offer = OfferModel();
var offerId;
RxList<OfferModel> offerList = <OfferModel>[].obs;
var isLoading = true.obs;
//rest of the code
}
我无法理解你的代码,但我可以理解你的问题如下。
你有这样一个对象
Object{
field1 String,
field2 List<String>
}
在您的 Firestore 中,您拥有相应对象的数据。现在您想知道如何获取 field2 数组。
所以这可以使用 List.castFrom(data['field2']).
来实现例如
QuerySnapshot<Map<String, dynamic>> data = await FirebaseFirestore.instance.collection('object').get();
List<Object> objList = data.docs.map<Object>((data) =>
new Shop(
field1: data['field1'],
field2: List.castFrom(data['field2'])
)
).toList();