Flutter
Parse SDK (REST)

Files

13min

Save Files from a Flutter App

Introduction

Sometimes applications need to store data that is often too large to be stored inside a ParseObject. The most common use case is storing images, but you can also use it for documents, videos, music, and other binary data.

To store a file on Parse, you should always associate the file with another data object so you can retrieve this file path when querying the object. If you do not associate, the file will be stored, but you will not find them on the Cloud. Another important tip is to give a name to the file that has a file extension. This extension lets Parse figure out the file type and handle it accordingly. We should also mention that each upload gets a unique identifier, so there’s no problem uploading multiple files using the same name.

In Flutter, ParseFile and ParseWebFile let you store and retrieve application files in the Cloud that would. This guide explains how to store and retrieve files in your Flutter App to manage Back4app cloud storage.

  • If you do not associate your file to a data object the file will become an orphan file and you wont be able to find it on Back4App Cloud.


Prerequisites

  • An app created on Back4App:
  • An Flutter app connected to Back4app.
  • A device (or virtual device) running Android or iOS.
  • In order to run this guide example you should set up the plugin image_picker properly. Do not forget to add permissions for iOS in order to access images stored in device. {: .btn target=”_blank” rel=”nofollow”}. Carefully read the instructions for setting up the Android and iOS project.

Goal

Create an Flutter Gallery App that uploads and displays images from Back4app.

1 - Understanding ParseFile and ParseWebFile class

There are three different file classes in this Parse SDK for Flutter

  • ParseFileBaseis an abstract class, the foundation of every file class that this SDK can handle.
  • ParseFileextends ParseFileBase and is by default used as the file class on every platform (not valid for web). This class uses a File from dart:io for storing the raw file.
  • ParseWebFileis the equivalent to ParseFile used at Flutter Web. This class uses an Uint8List for storing the raw file.

The methods available on ParseFileBase to manipulate files:

  • save()or upload() for save file on Cloud
  • download()for retrive file and store in local storage

There are properties to get information from the saved file:

  • url: Gets the file url. It is only available after you save the file or after you get the file from a Parse.Object.
  • name: Gets the file name. This is the filename given by the user before calling the save() method. After callint the method, the property receives a unique identifier.

2 - Uploading an Image

To upload an image, you will only need to create a ParseFileBase instance and then call the save method.

Let’s do that in our upload function:

JS


The above snippet creates and saves the image, and after the save completes, we associate it with a ParseObject called Gallery.

JS


3 - Displaying Images

To display images, you need to get the image’s URL.

To upload an image, you will only need to create a ParseFileBase instance and then call the save method.

JS


4 - Upload and Retrieve from Flutter App

Let’s now use our example for uploading and displaying images in Flutter App, with a simple interface.

Open your Flutter project, go to the main.dart file, clean up all the code, and replace it with:

Dart


Find your ApplicationId and Client Key credentials navigating to your app Dashboard->Settings->Security and Keys at Back4App Website.

Update your code in main.dart with BOTH values of your project’s ApplicationId and ClientKey in Back4app.

  • keyApplicationId = AppId
  • keyClientKey = Client Key

Run the project, and the app will load as shown in the image.

Document image

Document image

Document image

Document image


Conclusion

At this point, you have uploaded image on Back4App and displayed it in a Flutter application.