运行 flutter app 在 assembleDebug 任务上抛出异常。尝试从未在任何地方指定的不存在路径读取文件

Exception is thrown when running flutter app, on assembleDebug Task. Tries to read file from non existing path that's not specified anywhere

Error

Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
Running Gradle task 'assembleDebug'...
E/flutter ( 6095): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Unable to load asset: assets/

这是突然发生的,抛出异常后,任务仍在运行,当应用程序最终启动时,它只是一个空白屏幕。错误中的路径不存在,我认为我的代码中没有提到上述路径,因为应用程序 运行 昨天就好了。

我试过运行 flutter clean 和 flutter pub get,flutter pub upgrade 都没有用。

错误输出中唯一读取文件的代码是这个,它在整个程序中只在 main 函数中运行一次。

class NiveisComida{
  static final NiveisComida _singleton = NiveisComida._internal();
  static Map<String,bool> nivel1 = {},nivel2 = {},nivel3 = {};

  static void _readNiveis() async {
    for(int i = 1;i<=3;i++){
      List<String> content = (await rootBundle.loadString('_ficheiros_extra/nivel$i.comida')).split('\n');
      Map<String,bool> aux ={};
      for(String linha in content){
        bool val = true;
        if(linha.contains(';bad')) val = false;
        aux.putIfAbsent(linha, () => val);
      }
      switch(i){
        case 1:
          nivel1 = aux;
          break;
        case 2:
          nivel2 = aux;
          break;
        case 3:
          nivel3 = aux;
          break;

      }
    }
  }

  factory NiveisComida() {
    _readNiveis();
    return _singleton;
  }

  NiveisComida._internal();
}

main.dart


void main() {
  WidgetsFlutterBinding.ensureInitialized();
  //NiveisComida(); //inicializar lista de niveis de comida
  Intl.defaultLocale = 'pt_PT';
  if(kIsWeb){
    runApp(funcaoMain());
  }else{
    initializeDateFormatting().then((_)=>runApp(funcaoMain()));
  }
}

pubspec.yaml:

flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true
  assets:
    - _ficheiros_extra/nivel1.comida
    - _ficheiros_extra/nivel2.comida
    - _ficheiros_extra/nivel3.comida
    - _ficheiros_extra/comidas.csv
    - _ficheiros_extra/
    - _ficheiros_extra/app_store_badges/

_ficheiros_extra/ 文件结构:

.
├── README.md
├── _ficheiros_extra
│   ├── alcool.csv
│   ├── app_store_badges
│   │   ├── apple-badge-tamanho-igual.png
│   │   ├── apple-badge.png
│   │   └── google-play-badge.png
│   ├── comidas.csv
│   ├── nivel1.comida
│   ├── nivel2.comida
│   └── nivel3.comida
├── analysis_options.yaml
├── android
│   ├── app

如果有帮助,我 运行 在 M1 Mac

您的错误表明您的代码试图加载 assets/nivel1.comida 不存在的内容。因为正确的路径是_ficheiros_extra/nivel1.comida。确保代码中的路径包含 _ficheiros_extra。您可以在加载之前打印您的路径以确保它。

重启模拟器即可解决。