flutter firebase firestore: How to get images stored in array firestore?

0

Issue

I want to display the images with index through the urls stored in Firebase firestore, I am getting the images stored in cloud storage but I don’t know how to get the url stored in firebase firestore which is in array. pls help me out as I am only a beginner and learning through internet. Image attached below. "users" then "userid" then "images"Thanks in advance.

List<String> urls = [];

  @override
  void initState() {
  super.initState();

 _getImages();
 
}

This is the container where images are displaying through urls stored in cloud storage.

                Container(

                  height: 160.0,
                  padding: EdgeInsets.symmetric(vertical: 15.0,horizontal: 15.0),

                    child: CustomScrollView(
                      
                      physics: const BouncingScrollPhysics(),


                      slivers: <Widget>[
                        SliverPadding(

                          padding: const EdgeInsets.all(10),
                          sliver:
                          _buildContent(urls),

                        )
                      ],
                    ),
                ),

This is method through which I am getting the images stored in cloud storage.

_getImages() async {

ListResult _result = await _stoarge.ref().child("images").list(ListOptions(maxResults: 10));
_result.items.forEach((_ref) async {
  String _u = await _ref.getDownloadURL();

  urls.add(_u);
  setState(() {
    print(urls.length);
    print(urls);
  });
});

 }

Solution

Put this in your getImages() function

//First, you must retrieve your document, like this:
DocumentSnapshot firebaseDoc = await FirebaseFirestore.instance.collection('users').doc('yourDocumentId').get();

//then you need to extract your list, which is in the 'images' field, you can do so like this:
List<String> imagesList = firebaseDoc.data()['images'];

//Now, you have a list, containing the URLs you want.
print(imagesList[0]); //should print the first URL, you can take it from here

Answered By – Huthaifa Muayyad

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More