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:
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.
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.
Comments