Beginning FlutterFire

Aug 30 2022 · Dart 2.16, Flutter 3.0, Visual Studio Code 1.69

Part 4: Save Pictures to Cloud Storage

20. Download Data from Cloud Storage

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 19. Upload Data to Cloud Storage Next episode: 21. Leverage Firebase Cloud Messaging

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 20. Download Data from Cloud Storage

Deleting files from your storage bucket is a straightforward process: basically, you just need to know the full path of the file you need to delete, and call the delete method: then, if the file exists, it gets deleted.

So, get back to the FirebaseHelper class, and add a new method, called deleteImage, that takes the path of the file you want to delete.

As usual the remote path will take activityimage and the basename of the path that was passed. Then, on the storage instance, call the ref method passing the path, and then delete. That’s it

Future deleteImage(String path) async { 
    path = 'activity_images/' + basename(path); 
    storage.ref(path).delete(); 
  } 

Now we need to call this method from the UI. So, when the user swipes left or right on the Dismissible widget, we need to call this method if there is a path that’s been already set.

So, get back to the Activities Screen, and in the ondismissed method of the Dismissible, check whether the imagePath of the current activity is not empty: and if it’s not, call the deleteImage on the helper instance, passing the imagePath with the bang operator, as this will never be null.

if (activity.imagePath != '' || activity.imagePath != null) {
  helper.deleteImage(activity.imagePath!); 
} 

Right, let’s run the app again, and delete an activity with an image already set. The activity disappears from the screen, but let’s make sure it was deleted from the bucket as well. And as you can see, the file that was removed from the app has been deleted from the storage as well.

Great. That completes our tasks with files. Let’s have a look at another at how to deal with notifications next!