获取我的Flatter应用程序时格式化firebase Firestore数据时出现问题
我是firebase的新手,我正在尝试使用“故事”插件在我的Flitter应用程序中实现类似instagram的故事 我试图将此数据称为: 我遇到的麻烦是试图找到一种方法来获取和格式化“文件”数组中的数据 这是我当前的代码: pubspec.yaml:获取我的Flatter应用程序时格式化firebase Firestore数据时出现问题,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我是firebase的新手,我正在尝试使用“故事”插件在我的Flitter应用程序中实现类似instagram的故事 我试图将此数据称为: 我遇到的麻烦是试图找到一种方法来获取和格式化“文件”数组中的数据 这是我当前的代码: pubspec.yaml: dependencies: story: ^0.4.0 故事模型: class StoryModel { final String displayName; final String avatarUrl; final Stri
dependencies:
story: ^0.4.0
故事模型:
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']
);
}
}
故障部分:
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
),
),
)
]),
],
);
},
),
);}
}
)
FutureBuilder(
future:storyRef.where('canView',arrayContains:currentUserModel.uid).get(),
生成器:(上下文,捕捉){
如果(!snap.hasData){
返回中心(子项:文本('error'),);
}否则{
QuerySnapshot snapshot=snap.data;
List storyPosts=snapshot.docs.map((doc)=>StoryModel.fromDocument(doc)).toList();
返回容器(
页边空白:仅限边集(前10页),
身高:150,
子项:ListView.builder(
物品计数:6,
填充:仅限边缘设置(左:28),
滚动方向:轴水平,
物理:弹跳CrollPhysics(),
itemBuilder:(上下文,索引){
返回行(
儿童:[
栏(儿童:[
手势检测器(
子:容器(
身高:100,
宽度:100,
保证金:
仅限边缘设置(右:20),
装饰:盒子装饰(
边界:边界(
颜色:颜色,蓝色,
宽度:3),
形状:BoxShape.circle,
颜色:颜色。灰色
)
),
onTap:(){
打印(“导航到故事视图”);
},
),
填充物(
填充:仅限边缘设置(
顶部:5,右侧:22),
子:文本(
“插入名称”,
风格:GoogleFonts.lato(
颜色:颜色。蓝色[800],
尺寸:20,
fontWeight:fontWeight.w700
),
),
)
]),
],
);
},
),
);}
}
)
这段代码可以很好地获取“故事模型”数据,但我仍然需要一种从Firestore的每个“文件”获取“故事文件”数据的方法,并且我无法找到一段以我希望的方式工作的代码
因此,如果可能的话,我需要一种方法从每个单独的文档中获取“故事文件”列表,最好使用.fromdocument方法作为“故事模型”类的一部分。对模型稍作修改即可。现在您可以获得
故事文件的列表
,并可以访问它的属性
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']
);
}
}
类故事模型{
最终字符串显示名;
最后的字符串avatarUrl;
最终字符串所有者ID;
最终清单文件;
故事模型({this.displayName,this.avatarUrl,this.ownerId,this.file});
factory StoryModel.fromDocument(DocumentSnapshot文档){
///在返回[StoryModel]实例之前制作文件列表
List List=(doc.data()['file']作为List.map((e)=>StoryFile.fromMap(e)).toList();
返回故事模型(
displayName:doc.data()['displayName']??',
ownerId:doc.data(),
avatarUrl:doc.data(),
文件:列表,
);
}
}
类故事文件{
最终字符串文件类型;
最终字符串mediaUrl;
最后的字符串posted;
故事文件({this.mediaUrl、this.postId、this.filetype});
factory StoryFile.fromMap(地图文档){
返回故事文件(
文件类型:doc['filetype'],
mediaUrl:doc['mediaUrl'],
postId:doc['postId']
);
}
}