trouble formatting firebase Firestore data when getting for my flutter app

RyanBuss01

I'm new to firebase and I'm trying to implement instagram like stories to my flutter app using the "story" plugin.

I am trying to call this data:

enter image description here

the trouble I am having is trying to find a way to get and format the data in the "file" array.

this is my current code:

pubspec.yaml:

dependencies:
  story: ^0.4.0

story models:

class StoryModel {
  final String displayName;
  final String avatarUrl;
  final String ownerId;
  final List file;

  StoryModel({this.displayName, this.avatarUrl, this.ownerId, this.file});

  factory StoryModel.fromDocument(DocumentSnapshot doc){
    return StoryModel(
      displayName: doc.data()['displayName'] ?? '',
      ownerId: doc.data()['ownerId'] ?? '',
      avatarUrl: doc.data()['avatarUrl'] ?? '',
      file: doc.data()['file'] as List,
    );
  }
}

class StoryFile {
  final String filetype;
  final String mediaUrl;
  final String postId;

  StoryFile({this.mediaUrl, this.postId, this.filetype});

  factory StoryFile.fromDocument(DocumentSnapshot doc){
    return StoryFile(
      filetype: doc.data()['filetype'],
      mediaUrl: doc.data()['mediaUrl'],
      postId: doc.data()['postId']
    );
  }
}

trouble section:

                      FutureBuilder(
                        future: storyRef.where('canView', arrayContains: currentUserModel.uid).get(),
                        builder: (context, snap) {
                          if(!snap.hasData) {
                            return Center(child: Text('error'),);
                          } else {
                            QuerySnapshot snapshot = snap.data;
                            List<StoryModel> storyPosts = snapshot.docs.map((doc) => StoryModel.fromDocument(doc)).toList();
                          return Container(
                          margin: EdgeInsets.only(top: 10),
                          height: 150,
                          child: ListView.builder(
                            itemCount: 6,
                            padding: EdgeInsets.only(left: 28),
                            scrollDirection: Axis.horizontal,
                            physics: BouncingScrollPhysics(),
                            itemBuilder: (context, index) {
                              return Row(
                                children: [
                                  Column(children: [
                                    GestureDetector(
                                      child: Container(
                                        height: 100,
                                        width: 100,
                                        margin:
                                        EdgeInsets.only(right: 20),
                                        decoration: BoxDecoration(
                                            border: Border.all(
                                                color: Colors.blue,
                                                width: 3),
                                            shape: BoxShape.circle,
                                            color: Colors.grey
                                            )
                                      ),
                                      onTap: () {
                                        print('Navigate to story View');
                                      },
                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(
                                          top: 5, right: 22),
                                      child: Text(
                                        'insert name',
                                        style: GoogleFonts.lato(
                                            color: Colors.blue[800],
                                            fontSize: 20,
                                            fontWeight: FontWeight.w700
                                        ),
                                      ),
                                    )
                                  ]),
                                ],
                              );
                            },
                          ),
                        );}
                        }
                      )

This code works fine getting the "StoryModel" data but I still need a way to get the "StoryFile" data from each individual "file" from Firestore and I can't figure out a code that works in the way I want it to.

so I need a way to get a List of "StoryFile" from each individual document preferably in the .fromdocument method as part of the "StoryModel" class if possible.

Jagraj Singh

A slight change in your model can do the work for you. Now you can get a list of StoryFile and can access properties of it.

class StoryModel {
  final String displayName;
  final String avatarUrl;
  final String ownerId;
  final List<StoryFile> file;

  StoryModel({this.displayName, this.avatarUrl, this.ownerId, this.file});

  factory StoryModel.fromDocument(DocumentSnapshot doc){
///make list of files before returning [StoryModel] instance
 List<StoryFile> list = (doc.data()['file'] as List).map((e)=>StoryFile.fromMap(e)).toList();

    return StoryModel(
      displayName: doc.data()['displayName'] ?? '',
      ownerId: doc.data()['ownerId'] ?? '',
      avatarUrl: doc.data()['avatarUrl'] ?? '',
      file: list,
    );
  }
}

class StoryFile {
  final String filetype;
  final String mediaUrl;
  final String postId;

  StoryFile({this.mediaUrl, this.postId, this.filetype});

  factory StoryFile.fromMap(Map doc){
    return StoryFile(
      filetype: doc['filetype'],
      mediaUrl: doc['mediaUrl'],
      postId: doc['postId']
    );
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

error when i integrate my flutter app with firebase

getting firebase campaign information from flutter app

Flutter Firebase firestore .data[""] error

Firestore: Some values from my flutter app are not uploaded to Firestore database

Flutter: Not stable output in getting my array of data in Firestore when going back

Trying to use firebase for a get request for my flutter app and getting error

When getting data from Firebase Firestore my SwiftUI application crashes with an error. What could this be caused by?

cloud Firestore connect my flutter windows app

How to filter items in flutter when getting data from firebase

I am having trouble getting my app to detect a collision

Trouble formatting images in my CSS

firebase replaced my data when restarting my app

data is not getting render in Text() widget,how do i view data in my Flutter app

When I try to use firebase in my flutter app it produces an error

Retrieve data from a firestore - Firebase - Flutter

Firebase firestore getting data from inside collection

having trouble to run my first flutter app

My Flutter app doesnt compile anymore due to cloud firestore and firebase auth errors

Why data is not showing in my app, when I can see it in Firebase?

When i use firebase dynamic link in my flutter it opne app but not get link when app was kill

Getting "Uncaught TypeError: can't convert undefined to object" error when asking for data in firebase firestore

Getting an error when trying to retrieve data from Firebase FireStore document

I am getting this E/flutter (25055) error when I am running my App (Flutter , FireBase

Getting the right formatting for my XML data

flutter/firebase/dart: get data from firestore

Flutter - Adding data to Firebase Firestore with Model

Getting {"_h": 0, "_i": 0, "_j": null, "_k": null} when trying to get data from firebase firestore

Why my data is not getting into to the firestore cloud storage

Why my Firebase app writes data to the wrong Firestore database?