First flutter image displays, but not the second

997

In your pubspec.yaml, you should use:

- assets/images/

instead of

- images/

Edit regarding your update

I looked back at your code and updated comments, you need to change some code in a few places.

  • First, in your Home_widget.dart, replace Image.asset('images/museumInterior.png') and Image.asset('images/TMORA.png')

with Image.asset('assets/images/museumInterior.png') and Image.asset('assets/images/TMORA.png') respectively.

  • Then in your pubspec.yaml file, under assets:, only keep the entry - assets/images/ and remove the others.

  • Finally, run flutter clean on your project and rebuild.

Let me know if this helps.

Share:
997
heather
Author by

heather

Updated on December 23, 2022

Comments

  • heather
    heather over 1 year

    Problem

    TMORA.png is not displaying.

    Walkthrough

    Code

    • Images appear here in my code:

    Error

    • Error thrown by flutter:

    The asset images/TMORA.png does not exist. Try creating the file or fixing the path to the file. dart(asset_does_not_exist) [51,8]

    • The image does exist, though. I can see it when I click on the assets/images folder.

    enter image description here

    • I believe the problem stems from my yaml file (shown next).

    yaml file

    • The complete code (with the TMORA.png line commented out to remove the error) appears on Github.

    Note: I have two folders called images in this package. One is the project assets folder that contains an images folder with the 2 images to display on the app screen. The second is an images folder that holds images for my github readme. Am I confusing it?

    • pubspec.yaml file:

    name: second_app_Heather
    description: A new Flutter project.
    
    # The following line prevents the package from being accidentally published to
    # pub.dev using `pub publish`. This is preferred for private packages.
    publish_to: 'none' # Remove this line if you wish to publish to pub.dev
    
    # The following defines the version and build number for your application.
    # A version number is three numbers separated by dots, like 1.2.43
    # followed by an optional build number separated by a +.
    # Both the version and the builder number may be overridden in flutter
    # build by specifying --build-name and --build-number, respectively.
    # In Android, build-name is used as versionName while build-number used as versionCode.
    # Read more about Android versioning at https://developer.android.com/studio/publish/versioning
    # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
    # Read more about iOS versioning at
    # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
    version: 1.0.0+1
    
    environment:
      sdk: ">=2.7.0 <3.0.0"
    
    dependencies:
      flutter:
        sdk: flutter
    
    
      # The following adds the Cupertino Icons font to your application.
      # Use with the CupertinoIcons class for iOS style icons.
      cupertino_icons: ^0.1.3
    
    dev_dependencies:
      flutter_test:
        sdk: flutter
    
    # For information on the generic Dart part of this file, see the
    # following page: https://dart.dev/tools/pub/pubspec
    
    # The following section is specific to Flutter.
    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
    
      # To add assets to your application, add an assets section, like this:
      assets:
         - images/
         - images/museumInterior.png
         - images/TMORA.png
        #- images/TMORA.png
      #   - images/a_dot_ham.jpeg
    
      # An image asset can refer to one or more resolution-specific "variants", see
      # https://flutter.dev/assets-and-images/#resolution-aware.
    
      # For details regarding adding assets from package dependencies, see
      # https://flutter.dev/assets-and-images/#from-packages
     
    
      # To add custom fonts to your application, add a fonts section here,
      # in this "flutter" section. Each entry in this list should have a
      # "family" key with the font family name, and a "fonts" key with a
      # list giving the asset and other descriptors for the font. For
      # example:
      # fonts:
      #   - family: Schyler
      #     fonts:
      #       - asset: fonts/Schyler-Regular.ttf
      #       - asset: fonts/Schyler-Italic.ttf
      #         style: italic
      #   - family: Trajan Pro
      #     fonts:
      #       - asset: fonts/TrajanPro.ttf
      #       - asset: fonts/TrajanPro_Bold.ttf
      #         weight: 700
      #
      # For details regarding fonts from package dependencies,
      # see https://flutter.dev/custom-fonts/#from-packages

    EDIT:

    In response to @Anis R.'s suggestion (because I imagine a lot of people will suggest this), I have made an effort to implement that suggestion and recieve the following errors:

    Anis R. suggestion 1a

    Anis R. suggestion 1b

    Anis R. suggestion 1c

    Edit #2. Respose to Anis R's 2nd helpful comment.

    This was the problem... Internet resources are a little wishy-washy about where to place images that you want to use on your iPhone/android buttons, backgrounds, and other widgets. I'm not sure what is conventional/correct.

    Some sources recommend placing photos inside an images folder inside the root (Orange arrow Example recommendation here). Some sources recommend placing photos inside an assets folder (Green arrow Example recommendation here- see hour 1, min 43). Some recommend placing photos inside an images folder inside the assets folder (Orange arrow - this is what I attempted).

    Different file locations

    So, when I adopted Anis R's code suggestions (which I think follow convention), I got this error message that explained what the problem was.

    AnisError

    Now, this seems like a hard error for me to overcome. If the .yaml file can't find the assets folder, then I'm just stumped. I'm new and I don't think that I can figure this out. I have definitely seen this error before.

    Where this led me was to wonder, "Well, if it can't see the assets folder, then how the hell is it displaying the museumInterior.png photo?" Both photos are located in the same folder. Or are they?

    enter image description here

    Nope. It has never been pulling images from the assets\images folder. It has no idea that the assets\images folder exists. Its been pulling pics from the images folder where I store photos for my github README.md. There's a lot of stuff in there. The image called museumInterior.png just happened to be in there too. I put a copy of the TMORA.png inside the images folder and it displays on my screen.

    I, like Visual Studio Code, will pretend that assets folder, with an images folder inside, doesn't even exist.

    I don't think that this is the convention, but it does run properly.

    Much thanks to @Anis R. for walking me through this! Sometimes you just gotta talk it out.