Skip to main content

Firebase ML Model Downloader

Notice

This page is archived and might not reflect the latest version of the FlutterFire plugins. You can find the latest information on firebase.google.com:

https://firebase.google.com/docs/ml/flutter/use-custom-models

Once installed, you can access the firebase_ml_model_downloader plugin by importing it in your Dart code:

import 'package:firebase_ml_model_downloader/firebase_ml_model_downloader.dart';

Before using the package, you must first have ensured you have initialized FlutterFire.

To create a new downloader instance, call the instance getter on FirebaseModelDownloader:

FirebaseModelDownloader downloader = FirebaseModelDownloader.instance;

By default, this allows you to interact with the downloader using the default Firebase App used whilst installing FlutterFire on your platform. If however you'd like to use a secondary Firebase App, use the instanceFor method:

FirebaseApp secondaryApp = Firebase.app('SecondaryApp');
FirebaseModelDownloader downloader = FirebaseModelDownloader.instanceFor(app: secondaryApp);

List downloaded models#

To access any locally downloaded models on the device, call the listDownloadedModels method. The response contains a list of any locally downloaded custom models:

List<FirebaseCustomModel> models = await FirebaseModelDownloader.instance.listDownloadedModels();

Custom model#

A custom model contains information such as the name, size and unique hash of the model. It also contains access to the local File on the device, allowing you to provide it to an interpreter plugin.

Once a custom model has been downloaded, you can access it using the FirebaseCustomModel class:

List<FirebaseCustomModel> models = await FirebaseModelDownloader.instance.listDownloadedModels();
models.forEach((model) {
print('Name: ${model.name}');
print('Size: ${model.size}');
print('Hash: ${model.hash}');
});

If you're working with a 3rd party interpreter such as tflite_flutter, use the returned file to access the model:

final interpreter = await tfl.Interpreter.fromAsset(
model.absolute
);

Downloading a model#

Once you have deployed a TensorFlow model, you can download them to your device using the getModel method, specifying the model name and download options.

FirebaseCustomModel model = await FirebaseModelDownloader.instance.getModel('myModel', FirebaseModelDownloadType.latestModel);

The above code will download the latest available myModel model to the device, since the FirebaseModelDownloadType.latestModel has been specified. The method accepts 3 download types for differing behavior:

TypeDescription
FirebaseModelDownloadType.localModelReturns the current model if present, otherwise triggers new download (or finds one in progress) and only completes when download is finished.
FirebaseModelDownloadType.localModelUpdateInBackgroundReturns the current model if present and triggers an update to fetch a new version in the background. If no local model is present triggers a new download (or finds one in progress) and only completes when download is finished.
FirebaseModelDownloadType.latestModelReturns the latest model. Checks if latest model is different from local model. If the models are the same, returns the current model. Otherwise, triggers a new model download and returns when this download finishes.

You can also provide specific conditions for the download, such as whether the model should be downloaded whilst the device has access to cellular data, or if the device is charging.

To provide conditions, provide a FirebaseModelDownloadConditions instance to the getModel method:

// The following are the default conditions:
FirebaseModelDownloadConditions conditions = FirebaseModelDownloadConditions(
// Download whilst connected to cellular data
iosAllowsCellularAccess: true,
// Allow downloading in the background
iosAllowsBackgroundDownloading: false,
// Only download whilst charging
androidChargingRequired: false,
// Only download whilst on Wifi
androidWifiRequired: false,
// Only download whilst the device is idle
androidDeviceIdleRequired: false,
);
FirebaseCustomModel model = await FirebaseModelDownloader.instance.getModel('myModel', FirebaseModelDownloadType.latestModel, conditions);

Deleting a download model#

To delete a model from the device, call the deleteDownloadedModel method:

await FirebaseModelDownloader.instance.deleteDownloadedModel('myModel');

Note; this does not delete the model from your Firebase project, only the local copy on the device. You can download it again at any time.