FirebaseModelDownloader.instance.getModel("yourModelName",FirebaseModelDownloadType.localModel,FirebaseModelDownloadConditions(iosAllowsCellularAccess:true,iosAllowsBackgroundDownloading:false,androidChargingRequired:false,androidWifiRequired:false,androidDeviceIdleRequired:false,)).then((customModel){// Download complete. Depending on your app, you could enable the ML// feature, or switch from the local model to the remote model, etc.// The CustomModel object contains the local path of the model file,// which you can use to instantiate a TensorFlow Lite interpreter.finallocalModelPath=customModel.file;// ...});
许多应用会通过其初始化代码启动下载任务,您也可以在需要使用该模型之前随时启动下载任务。
3. 利用输入数据进行推断
现在,设备上已有模型文件,接下来您可以将其与 TensorFlow Lite 解释器搭配使用来进行推理。在缺少适用于 Dart 的受维护 TensorFlow Lite 库的情况下,您需要与适用于 iOS 和 Android 的原生 TensorFlow Lite 库集成。
附录:模型的安全性
无论您以何种方式在 Firebase ML 中添加自己的 TensorFlow Lite 模型,Firebase ML 都会以标准序列化的 protobuf 格式将这些模型存储到本地存储空间中。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-04。"],[],[],null,["# Use a custom TensorFlow Lite model with Flutter\n\n\u003cbr /\u003e\n\nIf your app uses custom\n[TensorFlow Lite](https://www.tensorflow.org/lite/) models, you can\nuse Firebase ML to deploy your models. By deploying models with Firebase, you\ncan reduce the initial download size of your app and update your app's ML models\nwithout releasing a new version of your app. And, with Remote Config and A/B\nTesting, you can dynamically serve different models to different sets of users.\n\nTensorFlow Lite models\n----------------------\n\nTensorFlow Lite models are ML models that are optimized to run on mobile\ndevices. To get a TensorFlow Lite model:\n\n- Use a pre-built model, such as one of the [official TensorFlow Lite models](https://www.tensorflow.org/lite/models)\n- [Convert a TensorFlow model, Keras model, or concrete function to TensorFlow Lite.](https://www.tensorflow.org/lite/convert)\n\nNote that in the absence of a maintained TensorFlow Lite library for Dart, you\nwill need to integrate with the native TensorFlow Lite library for your\nplatforms. This integration is not documented here.\n\nBefore you begin\n----------------\n\n1. [Install and initialize the Firebase SDKs for Flutter](/docs/flutter/setup)\n if you haven't already done so.\n\n2. From the root directory of your Flutter project, run the following\n command to install the ML model downloader plugin:\n\n flutter pub add firebase_ml_model_downloader\n\n3. Rebuild your project:\n\n flutter run\n\n1. Deploy your model\n--------------------\n\nDeploy your custom TensorFlow models using either the Firebase console or\nthe Firebase Admin Python and Node.js SDKs. See\n[Deploy and manage custom models](/docs/ml/manage-hosted-models).\n\nAfter you add a custom model to your Firebase project, you can reference the\nmodel in your apps using the name you specified. At any time, you can deploy a\nnew TensorFlow Lite model and download the new model onto users' devices by\ncalling `getModel()` (see below).\n\n2. Download the model to the device and initialize a TensorFlow Lite interpreter\n--------------------------------------------------------------------------------\n\nTo use your TensorFlow Lite model in your app, first use the model downloader\nto download the latest version of the model to the device. Then, instantiate a\nTensorFlow Lite interpreter with the model.\n\nTo start the model download, call the model downloader's `getModel()` method,\nspecifying the name you assigned the model when you uploaded it, whether you\nwant to always download the latest model, and the conditions under which you\nwant to allow downloading.\n\nYou can choose from three download behaviors:\n\n| Download type | Description |\n|--------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `localModel` | Get the local model from the device. If there is no local model available, this behaves like `latestModel`. Use this download type if you are not interested in checking for model updates. For example, you're using Remote Config to retrieve model names and you always upload models under new names (recommended). |\n| `localModelUpdateInBackground` | Get the local model from the device and start updating the model in the background. If there is no local model available, this behaves like `latestModel`. |\n| `latestModel` | Get the latest model. If the local model is the latest version, returns the local model. Otherwise, download the latest model. This behavior will block until the latest version is downloaded (not recommended). Use this behavior only in cases where you explicitly need the latest version. |\n\nYou should disable model-related functionality---for example, grey-out or\nhide part of your UI---until you confirm the model has been downloaded. \n\n FirebaseModelDownloader.instance\n .getModel(\n \"yourModelName\",\n FirebaseModelDownloadType.localModel,\n FirebaseModelDownloadConditions(\n iosAllowsCellularAccess: true,\n iosAllowsBackgroundDownloading: false,\n androidChargingRequired: false,\n androidWifiRequired: false,\n androidDeviceIdleRequired: false,\n )\n )\n .then((customModel) {\n // Download complete. Depending on your app, you could enable the ML\n // feature, or switch from the local model to the remote model, etc.\n\n // The CustomModel object contains the local path of the model file,\n // which you can use to instantiate a TensorFlow Lite interpreter.\n final localModelPath = customModel.file;\n\n // ...\n });\n\nMany apps start the download task in their initialization code, but you can do\nso at any point before you need to use the model.\n\n3. Perform inference on input data\n----------------------------------\n\nNow that you have your model file on the device you can use it with the\nTensorFlow Lite interpreter to perform inference. In the absence of a maintained\nTensorFlow Lite library for Dart, you will need to integrate with the\n[native TensorFlow Lite libraries](https://www.tensorflow.org/lite)\nfor iOS and Android.\n\nAppendix: Model security\n------------------------\n\nRegardless of how you make your TensorFlow Lite models available to\nFirebase ML, Firebase ML stores them in the standard serialized protobuf format in\nlocal storage.\n\nIn theory, this means that anybody can copy your model. However,\nin practice, most models are so application-specific and obfuscated by\noptimizations that the risk is similar to that of competitors disassembling and\nreusing your code. Nevertheless, you should be aware of this risk before you use\na custom model in your app."]]