Flutter 在Streambuilder中组合两个流并返回单个ListView(颤振)
我正在尝试将两个不同集合中的两个流(第一个是“avvenienti”,第二个是“prospetive”)合并到一个StreamBuilder中,该StreamBuilder返回一个ListView 我尝试了许多选项(使用Rx、StreamBuilder返回一个Strembulder,返回一个ListView等等),但未能使它们适应我的项目 在ListView的children属性上,我只能添加一个列表:Flutter 在Streambuilder中组合两个流并返回单个ListView(颤振),flutter,dart,google-cloud-firestore,stream,Flutter,Dart,Google Cloud Firestore,Stream,我正在尝试将两个不同集合中的两个流(第一个是“avvenienti”,第二个是“prospetive”)合并到一个StreamBuilder中,该StreamBuilder返回一个ListView 我尝试了许多选项(使用Rx、StreamBuilder返回一个Strembulder,返回一个ListView等等),但未能使它们适应我的项目 在ListView的children属性上,我只能添加一个列表: return StreamBuilder( stream: Global.avv
return StreamBuilder(
stream: Global.avvenimentiRef.streamData(),
builder: (context, snap1) {
if (snap1.hasData) {
List<Avvenimento> avvenimenti = snap1.data;
return StreamBuilder(
stream: Global.prospettiveRef.streamData(),
builder: (context, snap2) {
if (snap2.hasData) {
List<Prospettive> prospettive = snap2.data;
return Scaffold(
//this is the single ListView i want to use
body: ListView(
primary: false,
padding: const EdgeInsets.only(top: 20, bottom: 20),
children:
prospettive.map((prospettiveId) => ProspettiveItem(prospettiveId: prospettiveId)).toList(),
avvenimenti
.map((avvenimentoId) =>
AvvenimentoItem(avvenimentoId: avvenimentoId))
.toList(),
),
);
} else {
return LoadingScreen();
}
},
);
} else {
return LoadingScreen();
}
},
);
}
返回StreamBuilder(
流:Global.avvenmentiref.streamData(),
生成器:(上下文,snap1){
if(snap1.hasData){
List avvenimenti=snap1.data;
返回流生成器(
流:Global.prospettiveRef.streamData(),
生成器:(上下文,snap2){
if(snap2.hasData){
List prospitive=snap2.data;
返回脚手架(
//这是我想要使用的单一列表视图
正文:ListView(
主要:错误,
填充:仅限常量边集(顶部:20,底部:20),
儿童:
prospetive.map((prospettiveId)=>ProspettiveItem(prospettiveId:prospettiveId)).toList(),
阿维尼门蒂酒店
.map((avvenimentoId)=>
AvvenimentoItem(avvenimentoId:avvenimentoId))
.toList(),
),
);
}否则{
返回加载屏幕();
}
},
);
}否则{
返回加载屏幕();
}
},
);
}
我的数据库:
lass Document<T> {
final FirebaseFirestore _db = FirebaseFirestore.instance;
final String path;
DocumentReference ref;
Document({this.path}) {
ref = _db.doc(path);
}
Future<T> getData() {
return ref.get().then((v) => Global.models[T](v.data()) as T);
}
Stream<T> streamData() {
return ref.snapshots().map((v) => Global.models[T](v.data()) as T);
}
Future<void> upsert(Map data) {
return ref.set(Map<String, dynamic>.from(data));
}
}
class Collection<T> {
final FirebaseFirestore _db = FirebaseFirestore.instance;
final String path;
CollectionReference ref;
Collection({this.path}) {
ref = _db.collection(path);
}
Future<List<T>> getData() async {
var snapshot = await ref.get();
return snapshot.docs
.map((doc) => Global.models[T](doc.data()) as T)
.toList();
}
Stream<List<T>> streamData() {
return ref.snapshots().map((list) =>
list.docs.map((doc) => Global.models[T](doc.data()) as T).toList());
}
}
lass文档{
最终FirebaseFirestore _db=FirebaseFirestore.instance;
最终字符串路径;
文献参考文献;
文档({this.path}){
ref=_db.doc(路径);
}
未来的getData(){
返回ref.get().then((v)=>Global.models[T](v.data())作为T);
}
streamData(){
返回ref.snapshots().map((v)=>Global.models[T](v.data())作为T);
}
未来upsert(地图数据){
返回参考集(映射自(数据));
}
}
类集合{
最终FirebaseFirestore _db=FirebaseFirestore.instance;
最终字符串路径;
收集参考文献;
集合({this.path}){
ref=_db.collection(路径);
}
Future getData()异步{
var snapshot=await ref.get();
返回snapshot.docs
.map((doc)=>Global.models[T](doc.data())作为T)
.toList();
}
streamData(){
返回参考快照().map((列表)=>
list.docs.map((doc)=>Global.models[T](doc.data())为T.toList());
}
}
型号:
class ElencoProspettive {
String fonte;
String title;
ElencoProspettive({this.fonte, this.title});
ElencoProspettive.fromMap(Map data) {
fonte = data['data'] ?? '';
title = data['title'] ?? '';
}
}
class Prospettive {
String id;
String titlePro;
List<ElencoProspettive> elenco;
Prospettive({this.id, this.titlePro, this.elenco});
factory Prospettive.fromMap(Map data) {
return Prospettive(
id: data['data'] ?? '',
titlePro: data['title'] ?? '',
elenco: (data['elenco'] as List ?? [])
.map((v) => ElencoProspettive.fromMap(v))
.toList());
}
}
class Global {
//
// App Data
//
static final String title = 'Annales';
static final FirebaseAnalytics analytics = FirebaseAnalytics();
static final Map models = {
Avvenimento: (data) => Avvenimento.fromMap(data),
Prospettive: (data) => Prospettive.fromMap(data),
};
static final Collection<Avvenimento> avvenimentiRef =
Collection<Avvenimento>(path: 'avvenimenti');
static final Collection<Prospettive> prospettiveRef =
Collection<Prospettive>(path: 'propsettive');
}
class elencoprospative{
弦方;
字符串标题;
elencoprospective({this.fonte,this.title});
elencoprospative.fromMap(地图数据){
fonte=数据[“数据”]??“;
title=数据['title']??“”;
}
}
类推进式{
字符串id;
字符串标题;
罗列埃伦科;
推进式({this.id,this.titlePro,this.elenco});
factory ProPetative.fromMap(地图数据){
返回推进式(
id:data['data']??',
标题:数据['title']??“,
elenco:(数据['elenco']作为列表??[]
.map((v)=>elencoprovative.fromMap(v))
.toList());
}
}
型号:
class ElencoProspettive {
String fonte;
String title;
ElencoProspettive({this.fonte, this.title});
ElencoProspettive.fromMap(Map data) {
fonte = data['data'] ?? '';
title = data['title'] ?? '';
}
}
class Prospettive {
String id;
String titlePro;
List<ElencoProspettive> elenco;
Prospettive({this.id, this.titlePro, this.elenco});
factory Prospettive.fromMap(Map data) {
return Prospettive(
id: data['data'] ?? '',
titlePro: data['title'] ?? '',
elenco: (data['elenco'] as List ?? [])
.map((v) => ElencoProspettive.fromMap(v))
.toList());
}
}
class Global {
//
// App Data
//
static final String title = 'Annales';
static final FirebaseAnalytics analytics = FirebaseAnalytics();
static final Map models = {
Avvenimento: (data) => Avvenimento.fromMap(data),
Prospettive: (data) => Prospettive.fromMap(data),
};
static final Collection<Avvenimento> avvenimentiRef =
Collection<Avvenimento>(path: 'avvenimenti');
static final Collection<Prospettive> prospettiveRef =
Collection<Prospettive>(path: 'propsettive');
}
类全局{
//
//应用程序数据
//
静态最终字符串标题='Annales';
静态最终FirebaseAnalytics analytics=FirebaseAnalytics();
静态最终地图模型={
Avvenimento:(数据)=>Avvenimento.fromMap(数据),
prospative:(数据)=>prospative.fromMap(数据),
};
静态最终收集AVvenientIREF=
集合(路径:“avvenementi”);
静态最终收集ProspetiveRef=
集合(路径:“Propsetive”);
}
从Rx
使用,因为您提到您已经尝试过了。StreamBuilder(
stream: CombineLatestStream.list([
Global.avvenimentiRef.streamData(),
Global.prospettiveRef.streamData(),
]),
builder: (context, snap1)
- 请注意,如果其中一个流发生更改,
将重新加载StreamBuilder
- 您可以使用
或snap1.data[0]
.ect访问流返回的数据snap1.data[1]
ListView(
primary: false,
padding: const EdgeInsets.only(top: 20, bottom: 20),
children:
[...(prospettive.map((prospettiveId) => ProspettiveItem(prospettiveId: prospettiveId)).toList()),
...(avvenimenti
.map((avvenimentoId) =>
AvvenimentoItem(avvenimentoId: avvenimentoId))
.toList())],
),
谢谢你的回复,但我总是发现自己在同一个问题上。也就是说:ListView儿童:接受1个参数,但我需要avvenimenti和Prospettivi。我更新了我的答案,你能试试吗?是的!!太棒了,非常感谢你!很乐意帮忙D