简体   繁体   中英

Flutter - Get array from Firestore with FutureBuilder (using query)

I'm confusing myself terribly here...what is the correct syntax for listing an array from Firestore with FutureBuilder if I need to perform a document (where) query?

Firestore Database Structure:

 - [companies]
   - [doc]

 - [gallery]
   - [doc]
    - [reference] <- (ie, companies/doc)
    - [gallery_images] // List these, but show only images that match where() query

Here is my current code:

Future<List<dynamic>> getGallery() async {
  var firestore = Firestore.instance;

  DocumentReference docRef = firestore.collection('companies').document(widget.docID);

  var ref = firestore.collection('galleries').where('companyRef', isEqualTo: docRef).getDocuments();

  return ref.get().then((datasnapshot) {
        if (datasnapshot.exists) {
        List<dynamic> imageArray = datasnapshot.data['gallery_images'].toList();
          return imageArray;
        } else {
          print("Please just work...");
        }
      },
    );
  }
FutureBuilder(
  future: getGallery(),
  builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return Center(
         child: Text("Loading..."),
        );
       } else {
    return GridView.builder(
    gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
    itemCount: *length*,
    itemBuilder: (BuildContext context, int index) {
       return ViewGalleryItemThumbnail(
         viewGalleryItem: snapshot.data[index],
         ....

Would really appreciate if someone can show me where I'm going wrong!

It's not exactly clear what your Cloud Firestore schema looks like, but this looks more likely to return you the correct list of dynamics. (Note that by adding a generic to the map you could return a list of specifically what you want - eg .map<SomeRealType>((snap) => snap.data['gallery_images']) .)

When you're stuck with FutureBuilder it's sometimes useful to revert temporarily to a stateful widget. Override initState , and call a slightly modified getGallery which doesn't return a value at the end, but assigns it to a member variable inside setState . Plenty of opportunity for print statements that way. Additionally, try to convert any .then s in an async function into a linear series of await s. It makes it easier to read (and your debug prints come out in a linear sensible order).

  Future<List<dynamic>> getGallery() async {
    var firestore = Firestore.instance;

    var docRef = firestore.collection('companies').document(widget.docID);

    var querySnap = await firestore
        .collection('galleries')
        .where('companyRef', isEqualTo: docRef)
        .getDocuments();

    return querySnap.documents
        .map((snap) => snap.data['gallery_images'])
        .toList();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM