FlutterError: Unable to load asset

后端 未结 22 3273
死守一世寂寞
死守一世寂寞 2020-11-30 10:31

This is the folder structure of my app

.idea
.vscode
android
build
fonts
 Oxygen-Bold.tff
 Oxygen-Light.tff
 Oxygen-Regular.tff
images
 pizza0.png
 pizza1.pn         


        
相关标签:
22条回答
  • 2020-11-30 11:26

    Encountered the same issue with a slightly different code. In my case, I was using a "assets" folder subdivided into sub-folders for assets (sprites, audio, UI).

    My code was simply at first in pubspec.yaml- alternative would be to detail every single file.

    flutter:
    
      assets:
        - assets
    
    

    Indentation and flutter clean was not enough to fix it. The files in the sub-folders were not loading by flutter. It seems like flutter needs to be "taken by the hand" and not looking at sub-folders without explicitly asking it to look at them. This worked for me:

    flutter:
    
      assets:
        - assets/sprites/
        - assets/audio/
        - assets/UI/
    
    

    So I had to detail each folder and each sub-folder that contains assets (mp3, jpg, etc). Doing so made the app work and saved me tons of time as the only solution detailed above would require me to manually list 30+ assets while the code here is just a few lines and easier to maintain.

    0 讨论(0)
  • 2020-11-30 11:26
    1. you should start image path with assets word:

    image: AssetImage('assets/images/pizza0.png')

    1. you must add each sub folder in a new line in pubspec.yaml
    0 讨论(0)
  • 2020-11-30 11:26

    I was also facing the same issue . The issue on my side was that the image name was having the space in between its name so I updated the image name with the new name that does not have any space.

    Image name creating the error was comboclr emtpy no circle.png

    I updated this name to comboclr_emtpy_no_circle.png

    0 讨论(0)
  • 2020-11-30 11:26

    I ran into this issue and very nearly gave up on Flutter until I stumbled upon the cause. In my case what I was doing was along the following lines

    static Future<String> resourceText(String resName) async
    {
     try
     { 
      ZLibCodec zlc = new ZLibCodec(gzip:false,raw:true,level:9);
      var data= await rootBundle.load('assets/path/to/$resName');
      String result = new 
      String.fromCharCodes(zlc.decode(puzzleData.buffer.asUint8List()));
      return puzzle;
     } catch(e)
     {
      debugPrint('Resource Error $resName $e');
      return ''; 
     } 
    }
    
    static Future<String> fallBackText(String textName) async
    {
     if (testCondtion) return 'Some Required Text';
     else return resourceText('default');
    } 
    

    where Some Required Text was a text string sent back if the testCondition was being met. Failing that I was trying to pick up default text from the app resources and send that back instead. My mistake was in the line return resourceText('default');. After changing it to read return await resourceText('default') things worked just as expected.

    This issue arises from the fact that rootBundle.load operates asynchronously. In order to return its results correctly we need to await their availability which I had failed to do. It strikes me as slightly surprising that neither the Flutter VSCode plugin nor the Flutter build process flag up this as an error. While there may well be other reasons why rootBundle.load fails this answer will, hopefully, help others who are running into mysterious asset load failures in Flutter.

    0 讨论(0)
  • 2020-11-30 11:27

    For me,

    1. flutter clean,
    2. Restart the android studio and emulator,
    3. giving full patth in my image

      image: AssetImage(
        './lib/graphics/logo2.png'
         ),
         width: 200,
         height: 200,
       );
      

    these three steps did the trick.

    0 讨论(0)
  • 2020-11-30 11:28

    Actually the problem is in pubspec.yaml, I put all images to assets/images/ and

    This wrong way

    flutter:
      uses-material-design: true
      assets:
      - images/
    

    Correct

    flutter:
      uses-material-design: true
      assets:
      - assets/images/
    
    0 讨论(0)
提交回复
热议问题