Home > Software design >  Null check operator error with tflite Flutter
Null check operator error with tflite Flutter

Time:09-17

The following code causes this error Null check operator used on a null value but I cannot figure out why.

import 'package:tflite_flutter/tflite_flutter.dart';

final modelFile = 'model.tflite';

void main() async {
  foo();
}

void foo() async {
  Interpreter inter = await Interpreter.fromAsset(modelFile);
}

The model exists at the correct location and I have tried multiple other locations just in case. The file is in my pubspec.yaml and I have tried multiple variations just in case. Not sure if I need to provide more parameters or something but I can't figure it out. Any help would be appreciated.

CodePudding user response:

It looks like you have not exported the file. To fix to go to pubspec.yaml and find these lines:

flutter:

  assets:
    - model.tflite

And make sure you are specifying the correct file location when adding it through pubscpec. After that uninstall the app, do flutter clean, flutter pub get and it should work

CodePudding user response:

I assumer the directory containing the model is named as "assets".

import 'package:tflite_flutter/tflite_flutter.dart';

final modelFile = 'assets/model.tflite';

void main() async {
  foo();
}

void foo() async {
  Interpreter inter = await Interpreter.fromAsset(modelFile);
}

Code for pubspec:-

flutter:

  assets:
    - assets/model.tflite
  • Related