How to access firebase collection

Viewed 56

I'm reading collections from firebase, string name, and image . an image URL from google Storge on FireBase

     _categoryReference.get().then((value) {
          for (int i = 0; i < value.docs.length; i++) {
            _categoryModel.add(CategoryModel.fromJson(value.docs[i].data()));
            print(value.docs[1].data()); 

on Console, I can print the values from the loop and browse Image Url, but when comes to display it on wiget ex. Image.network value is null, the name is successfully rendered

2 Answers

Try below code

QuerySnapshot query = await _categoryReference.get();

List<String> imageUrls = [];

query.docs.forEach((document) { 
  imageUrls.add(document.data()['imageUrl']); // change ['imageUrl'] to your key which is in document
});

setState((){});

I always did it this way:

final List<CategoryModel> categories = await _categoryReference.get((snapshot) => snapshot.docs.map((doc) => CategoryModel.fromJson(doc.data()).toList();
Related