San*_*dal 5 dart firebase flutter google-cloud-firestore
我正在尝试使用 Flutter 和 Firestore 构建一个应用程序,但我对我必须将数据从 Firestore 实现为 Dart 语言的方式感到困惑,所以问题是我的 Firestore 中有一个地图数组,每个元素在数组代表这样的地图
在我的应用程序中,我想将 Firestore 中的数据表示为可读的内容,我声明了一个这样的类:
class rest_model{
String rest_name;
GeoPoint rest_lat_long;
GeoPoint city_lat_long;
var rest_address = new Map();
var rest_food_list = new List<Map>();
Run Code Online (Sandbox Code Playgroud)
我知道如何将另一个转换为变量或可读,我使用这种方法:
rest_model.map(dynamic obj){
this.rest_name = obj['rest_name'];
this.rest_lat_long = obj['rest_lat_long'];
this.city_lat_long = obj['city_lat_long'];
this.rest_address['rest_email'] = obj['rest_address']['rest_email'];
this.rest_address['rest_phone']=obj['rest_address']['rest_phone'];
this.rest_address['rest_website'] = obj['rest_address']['rest_website'];
}
rest_model.fromMap(Map<String, dynamic> map){
this.rest_name = map['rest_name'];
this.rest_lat_long = map['rest_lat_long'];
this.city_lat_long = map['city_lat_long'];
this.rest_address['rest_email'] = map['rest_address']['rest_email'];
this.rest_address['rest_phone']=map['rest_address']['rest_phone'];
this.rest_address['rest_website'] = map['rest_address']['rest_website'];
}
Map<String, dynamic> toMap(){
var map = new Map<String, dynamic>();
map['rest_name'] = rest_name ;
map['rest_lat_long'] = rest_lat_long;
map['city_lat_long'] = city_lat_long;
map['rest_address'] = rest_address.values;
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何转换 map 的数组rest_food_list。
请任何人都可以帮助我吗?
小智 12
在 dart 中创建一个类似于 firestore1 中的类。在 dart 中创建一个类似于 firestore 中的类。我在 Dart 中有一个 Post 类,在 firestore 中有一个帖子数组。 地图的 Firestore 数组
我在 Dart 的课程如下所示
class Main{
final String userName;
final List<Post> post;
Main({this.userName, this.post});
}
class Post {
final String postImg;
final int likes;
final String description;
Post({this.postImg, this.description, this.likes});
}
Run Code Online (Sandbox Code Playgroud)
当我从 firestore 获取数据时,由于 firestore 有数组,因此您可以在 dart 中像这样转换 Map 数组。
//fetch firestore documents by getDocuments()
data.documents.forEach((doc) {
Main main = new Main(
userImg: doc.data["user_image"],
post: List<Post>.from(doc.data["posts"].map((item) {
return new Post(
likes: item["likes"],
postImg: item["image"],
description: item["description"]);
})));
//add the main data object to List;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!!
| 归档时间: |
|
| 查看次数: |
4950 次 |
| 最近记录: |