2

Pubspec.yaml

flutter:
  uses-material-design: true
  assets:
    - lib/images/
    - lib/images/app_logo.png

Main.dart

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          color: Colors.blueAccent,
          image: DecorationImage(
            image: ExactAssetImage('lib/images/app_logo.png'),
            fit: BoxFit.cover,
          ), //DecorationImage
        ), //BoxDecoration
      ),//Container
    ); //Scaffold
  }

No errors in app at all, just will not display anything. Have never seen this before. Have tried all manner of different indentations in pubspec. Have tried removing space between - and lib/images and replaced with tab.

What gives?

1
  • stop the run->place image in correct location->use flutter pub get on terminal or via IDE. ->run with correct path Commented Jul 4, 2019 at 16:05

3 Answers 3

4

Code is working perfectly, even inside lib folder. Let me share my code

Here is my pubspec.yaml code

name: nav
description: A new Flutter project.

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:

  uses-material-design: true

  assets:
    - lib/images/
    - lib/images/app_logo.jpg

my main.dart code

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp( home: Scaffold(
      appBar: AppBar(),
      body: Container(
        height: 200,
        decoration: BoxDecoration(
          color: Colors.blueAccent,
          image: DecorationImage(
            image: ExactAssetImage('lib/images/app_logo.jpg'),
            fit: BoxFit.cover,
          ), //DecorationImage
        ), //BoxDecoration
      ),//Container
    )); //Scaffold
  }
}

The solution you may try:

  1. Try to restart your app from the beginning (not by Shift + R)
  2. Make sure, you have given proper spacing & file name extensions correctly
Sign up to request clarification or add additional context in comments.

1 Comment

I really don't understand what is different here. I just moved the images back and forth and kept the same code, and now it is working fine. There is no real answer here, it just fully bugged out. I had thought that it was because I didn't give it a height or width, but now if I comment those lines out it still works. I have no idea, but this is very frustrating and I hope no one else goes through this. If anyone reading this is having trouble: try changing folders, try moving files into the app folder by dragging them into the folder in android studio instead of windows explorer.
1

Try removing the image from lib, put the image instead in a new folder called images (on same level of lib file) change your pubspec.yaml to:

flutter:
  uses-material-design: true
  assets:
    - images/app_logo.png

and in your code

image: ExactAssetImage('images/app_logo.png'),

I have tried this and it worked perfectly.

2 Comments

Have moved the file and made the suggested changes. Nothing happened. In all my previous apps this is my standard folder location. Out of curiosity, I copied a confirmed working image from a previously published app. Added images/app_image.png to my pubspec.yaml file. Changed code to use image: ExactAssetImage('images/app_image.png'), .... Nothing....
Are you sure you're doing get flutter packages and everything afterwards? Cause i swear i tried this same code copy paste + my changes and it worked perfectly fine
-1

I've had an issue with this same thing except my app kept displaying an older version of an asset image with the same name (I edited the image). I spent way too long trying to figure out what was wrong including, but not limited to, things such as flutter clean, wiping user data from my vm, renaming, etc. The renaming worked, which gave me a clue that it might be in the asset image cache itself not updating (as in: only creates new data when a new name is mapped). Finally, I CTRL-B into AssetImage which brought me into the image_resolution.dart file. After reading some of the code, I noticed it had a String constant with the filename of "AssetManifest.json". I did a search in the app directory of that file and came up with two locations.

...(your app)\build\app\intermediates\merged_assets\debug\out\flutter_assets

and

...(your app)\build\app\intermediates\flutter\debug\flutter_assets

In those two directories, you'll notice several files and directories. I made the AssetManifest.json into a .bak file to see if it got replaced on restart. It did, but still without results. So I backed up all the files in those two folders, then deleted them so that the folders were empty. When I restarted the app, the folders repopulated and the edited version of that picture displayed.

MY SOLUTION: Delete the contents in both flutter_assets folders in the paths I showed (NOT your app's local assets folder), and restart the app.

My guess is that flutter only updates the image cache when a new name is mapped to it. So changing the actual file that the name refers to does nothing. It seems as though deleting the contents of both folders, not just the manifest, forces your project to rebuild not only the manifest, but also the cache itself and associating the original file name with the new version of that file.

Hope that helps!

EDIT: I did have to perform a flutter clean before deleting the files. I also wiped the user data from the AVD just for good measure.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.