0

I found following issue. Then I understand it.

Flutter / FireStore: how to display an image from Firestore in Flutter?

File uploading is succeeding.

var imgUrl = await ref.getDownloadURL();
print(imgUrl.toString());

However I have following error. It seems I'm doing same.

Unhandled Exception: PlatformException(Error -13010, FIRStorageErrorDomain, Object images/cars/40711b90-9db4-11ea-c602-a557c9b7697a.jpeg does not exist.)

However I have no idea how to display and handle it.

Please give me advice. Thanks.

3
  • Do you have image store in FireBaseStorage and that image have proper Rules given to access them? Commented May 24, 2020 at 11:52
  • I am trying with same application. Rules should be same. rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } } Commented May 24, 2020 at 11:54
  • enter image path your browser and check image is shown or not, if shown everything is right Commented May 24, 2020 at 12:05

1 Answer 1

3

You need to add the url to firestore first:

          StorageTaskSnapshot snapshot = await storage
              .ref()
              .child("images/$imageName")
              .putFile(file)
              .onComplete;
          if (snapshot.error == null) {
            final String downloadUrl =
                await snapshot.ref.getDownloadURL();
            await Firestore.instance
                .collection("images")
                .add({"url": downloadUrl, "name": imageName});
             }

Now in Firestore you will have collection called images and document with the image url and image name. The method getDownloadUrl() returns the url of the image so you can store it in Firestore. Then to display it you can do the following:

body: Container(
  padding: EdgeInsets.all(10.0),
  child: FutureBuilder(
    future: getImages(),
    builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        return ListView.builder(
            shrinkWrap: true,
            itemCount: snapshot.data.documents.length,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(
                contentPadding: EdgeInsets.all(8.0),
                title:
                    Text(snapshot.data.documents[index].data["name"]),
                leading: Image.network(
                    snapshot.data.documents[index].data["url"],
                    fit: BoxFit.fill),
              );
            });
      } else if (snapshot.connectionState == ConnectionState.none) {
        return Text("No data");
      }
      return CircularProgressIndicator();
    },
  ),
),

/// code here

  Future<QuerySnapshot> getImages() {
    return fb.collection("images").getDocuments();
  }

Here you use the method getImages() which retrieves all the images from the collection images. To display the image you can use Image.network widget.

Sign up to request clarification or add additional context in comments.

8 Comments

I might take wrong design to store data in firestore. I don't have good idea to specify images. How should I hold url data in firestore?
I didn't understand what do you mean?
I would like to upload image into firestrage. Then retrieve the image soon to show. However now I have http URL in firedatabase.
The above code does exactly what you want, putFile is adding the image in firebase storage, add() is creating the document with the url of the image in firebase storage. In the second part of the code the image is retrieved and shown in the screen
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.