Flutter Dart如何回报未来<;列表<;MyProductTile>&燃气轮机;而不是列表<;未来<;MyProductTile>&燃气轮机;
我有一个异步函数Flutter Dart如何回报未来<;列表<;MyProductTile>&燃气轮机;而不是列表<;未来<;MyProductTile>&燃气轮机;,flutter,dart,flutter-provider,Flutter,Dart,Flutter Provider,我有一个异步函数getProducts() 到目前为止,我设法返回了一个列表: Future<List<MyProductTile>> getProducts( Stream<DocumentSnapshot> stream) async { var documentSnapshot = await stream.first; List<DocumentReference> productsDocRefsList =
getProducts()
到目前为止,我设法返回了一个列表
:
Future<List<MyProductTile>> getProducts(
Stream<DocumentSnapshot> stream) async {
var documentSnapshot = await stream.first;
List<DocumentReference> productsDocRefsList =
List<DocumentReference>.from(documentSnapshot.data['used_products']);
var x = productsDocRefsList
.map((documentReference) => documentReference.get().then(
(documentSnapshot) =>
MyProductTile.fromFirestore(documentSnapshot)))
.toList();
print(x.runtimeType); // List<Future<MyProductTile>>
}
未来产品(
流(流)异步{
var documentSnapshot=wait stream.first;
列出产品目录引用列表=
列表。来源(documentSnapshot.data[“二手产品]);
var x=productsDocRefsList
.map((documentReference)=>documentReference.get()。然后(
(文档快照)=>
MyProductTile.fromFirestore(documentSnapshot)))
.toList();
打印(x.runtimeType);//列表
}
我尝试使用Future.wait,但没有成功。一种方法是使用Dart API。这尤其有用,因为您将DocumentSnapshot
流作为函数的参数
Future<List<MyProductTile>> getProducts(Stream<DocumentSnapshot> stream) {
return stream
.expand((documentSnapshot) => documentSnapshot.data['used_products'])
.asyncMap((documentReference) => documentReference.get())
.map((documentSnapshot) => MyProductTile.fromFirestore(documentSnapshot))
.toList();
}
未来的getProducts(流){
回流
.expand((documentSnapshot)=>documentSnapshot.data['used_products'])
.asyncMap((documentReference)=>documentReference.get())
.map((documentSnapshot)=>MyProductTile.fromFirestore(documentSnapshot))
.toList();
}
通过等待每个期货来转换期货列表
futuredatafromfourtures(列表期货)异步{
列表数据列表=[];
forEach((Future i)async{dataList.add(wait i);});
返回数据列表;
}
}
容器(
孩子:未来建设者(
future:getDetails(),
生成器:(上下文,快照){
如果(snapshot.connectionState==
连接状态(正在等待){
返回中心(
子项:文本('Loading…'),
);
}否则{
返回ListView.builder(
//填充:边缘组。对称(水平:16.0),
收缩膜:对,
物理:ClampingScrollPhysics(),
itemCount:snapshot.data.length,
itemBuilder:(上下文,索引){
var sub_text_en=snapshot.data[index].data['sub_text_en'];
返回列(
textDirection:textDirection.rtl,
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
正文(
snapshot.data[index].data['sub_text_ar'],
textAlign:textAlign.right,
样式:新文本样式(
字体大小:14.0,
//颜色:颜色,蓝色,
容重:
重量(正常),
),
正文(
子文本包含('\n')?子文本:子文本替换所有('\\n','\n'),
textAlign:textAlign.left,
样式:新文本样式(
字体大小:14.0,
//颜色:颜色。蓝灰色,
fontWeight:fontWeight.bold),
),
],
);
});
}
}),
),
Future getDetails() async {
var firestore = Firestore.instance;
QuerySnapshot qs = await firestore
.collection('letters_sub')
.where('sub_id', isEqualTo: widget.letters.id)
.getDocuments();
return qs.documents;
Container(
child: FutureBuilder(
future: getDetails(),
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return Center(
child: Text('Loading...'),
);
} else {
return ListView.builder(
//padding: EdgeInsets.symmetric(horizontal: 16.0),
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
var sub_text_en = snapshot.data[index].data['sub_text_en'];
return Column(
textDirection: TextDirection.rtl,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
snapshot.data[index].data['sub_text_ar'],
textAlign: TextAlign.right,
style: new TextStyle(
fontSize: 14.0,
//color: Colors.blue,
fontWeight:
FontWeight.normal),
),
Text(
sub_text_en.contains('\n') ? sub_text_en : sub_text_en.replaceAll('\\n ', '\n') ,
textAlign: TextAlign.left,
style: new TextStyle(
fontSize: 14.0,
// color: Colors.blueGrey,
fontWeight: FontWeight.bold),
),
],
);
});
}
}),
),