0

I have an async function getProducts() that is supposed to return a Future<List<MyProductTile>> meant to a be fed to a FutureProvider.

So far, I managed to return a List<Future<MyProductTile>> instead:

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>>
}

I tried to use Future.wait as suggested in this stackoverflow answer but I didn't succeed.

4

3 回答 3

1

One way is to utilise asyncMap from the Dart Stream API. This is especially useful, because you are getting DocumentSnapshot stream as a parameter to your function.

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();
  }
于 2019-10-28T20:49:20.897 回答
0

Converting a List of Futures by awaiting every Future.

Future<List<Object>> dataFromFutures(List<Future<Object>> futures) async {
  List<Object> dataList = [];
  futures.forEach((Future i) async {dataList.add(await i);});
  return dataList;
}
于 2019-10-28T21:00:06.693 回答
0
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),
                            ),


                          ],
                        );
                      });
                }
              }),
        ),
于 2019-10-28T21:29:18.600 回答