Home > Enterprise >  Create a list of models from a list of data from documents
Create a list of models from a list of data from documents

Time:12-30

My flutter app is going to have a feed of posts from users. I have posts in my firebase firestore database from my swift app that I am going to replace. So for I have this in my flutter fetch posts function:

    final userRef = FirebaseFirestore.instance.collection('users');

        final QuerySnapshot result = await userRef.get();

        result.docs.forEach((res) async {
          print(res.id);
          QuerySnapshot posts = await userRef.doc(res.id).collection("posts").get();

          posts.docs.forEach((res) {
            print(res.data());
          });
        });

So with this function, I can print a list of the data of all of each users posts. Here is a part of the print:

flutter: {postedDate: Oct 24, 2021 at 6:10 AM, id: twentyonepilots_354_1635073829.595439, caption: , likers: [], postUrlString: https://firebasestorage.googleapis.com:443/v0/b/globe-e8b7f.appspot.com/o/twentyonepilots/posts/twentyonepilots_354_1635073829.595439.png?alt=media&token=48b6b1bc-112b-4d61-84ee-3847ba8dce94}
flutter: {postedDate: 13 Aug 2021, 9:54 AM, id: viktoria_923_1628837663.179892, caption: just posted, feeling good :), likers: [anelia, globetester, alis, devil, vladyynk, fllcuriie, HackerX, nihilistic, imherefromtiktok, donnasmithofficial, hhhh, sachiq], postUrlString: https://firebasestorage.googleapis.com:443/v0/b/globe-e8b7f.appspot.com/o/viktoria/posts/viktoria_923_1628837663.179892.png?alt=media&token=634ff70e-028c-4452-8c2b-b598eb8b8e48}
flutter: {postedDate: 21. Nov 2021 at 18:34, id: timsbs_464_1637516088.259705, caption: Hi https://google.com, likers: [timsbs], postUrlString: https://firebasestorage.googleapis.com:443/v0/b/globe-e8b7f.appspot.com/o/timsbs/posts/timsbs_464_1637516088.259705.png?alt=media&token=4ce3bf4d-3187-4ee9-954a-dc39532694ae}

How can I turn this list into a list of this post model?:

class Post {
  String id = "";
  String caption = "";
  String postUrlString = "";
  Timestamp postedDate = Timestamp.now();

  Post(
      {required this.id,
      required this.caption,
      required this.postUrlString,
      required this.postedDate});
}

CodePudding user response:

Adding a 'fromJson' method to Post class like below.

      final userRef = FirebaseFirestore.instance.collection('users');

      final QuerySnapshot result = await userRef.get();

      List<Post> listPosts = [];

      result.docs.forEach((res) async {
        print(res.id);
        QuerySnapshot posts = await userRef.doc(res.id).collection("posts").get();

        posts.docs.forEach((res) {
          listPosts.add(Post.fromJson(res.data() as Map<String, dynamic>));
        });

        print(listPosts);
      });
class Post {
  final String id;
  final String caption;
  final String postUrlString;
  final Timestamp postedDate;

  Post(
      {required this.id,
      required this.caption,
      required this.postUrlString,
      required this.postedDate});
  
  Post.fromJson(Map<String, dynamic> json)
    : id = json['id'],
      caption = json['caption'],
      postUrlString = json['postUrlString'],
      postedDate = json['postedDate'] != null ? Timestamp.fromDate(json['postedData']) : Timestamp.now();

  @override
  String toString() => '''Post { id: $id,
     caption: $caption,
     postUrlString: $postUrlString
     postedData: $postedData
  }''';
}
  • Related