如何获取地图的DocumentSnapshot并获取地图的长度?

时间:2020-07-06 12:14:14

标签: firebase flutter dart google-cloud-firestore

我想获取Map 的长度,如firebase的图片所示,并显示帖子的投票数。我如何首先获取该字段的documentSnapshot?我有显示的代码片段,但是有错误。

class Post { //for forums
  String title;
  String content;
  String timestamp;
  String imageurl;
  String username;
  String profilePicture;
  String documentid;
  String ownerid;
  Map<String, bool> saved = {};
  Map<String, bool> upvotes = {};
  //Map<String, bool> saved;


  Post(
    this.title,
    this.content,
    this.timestamp,
    this.imageurl,
    this.username,
    this.profilePicture,
    this.documentid,
    this.ownerid,
    this.saved,
    this.upvotes
    //this.image
  );

  Post.fromSnapshot(DocumentSnapshot snapshot) : 
  title = snapshot["title"],
  content = snapshot['content'],
  timestamp = snapshot['timestamp'],
  imageurl = snapshot['imageurl'],
  username = snapshot['username'],
  profilePicture = snapshot['profilePicture'],
  documentid = snapshot['documentid'],
  upvotes = snapshot['upvotes'], //gives error as shown in red image
  ownerid = snapshot['ownerid'],
  saved = snapshot['saved'];//gives error as shown in red image
}

Firebase image Code and error image

0 个答案:

没有答案