Flutter Image not being displayed

2,683

in the assetImage the synatx is usually like this :

image: AssetImage("images/loginBackground.png")

also if you didn't enable your assets in pubspec.yaml by doing this in your assets section :

 assets:
    - images/

also a little bit of error code will help

Share:
2,683
LWB
Author by

LWB

Updated on December 29, 2022

Comments

  • LWB
    LWB over 1 year

    So I'm trying to make a page with an image background and this is my code:

    body: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage("loginBackground"),
              ),
            )
          ),
    

    However when I run this it opens a new editor with some of Flutters code, and a line is highlighted yellow, which I think means a breakpoint (correct me if I'm wrong). The file name is 'image_provider.dart' and this is the relevant code (I have indicated the highlighted line):

    @protected
      Future<ui.Codec> _loadAsync(AssetBundleImageKey key, DecoderCallback decode) async {
        ByteData? data;
        // Hot reload/restart could change whether an asset bundle or key in a
        // bundle are available, or if it is a network backed bundle.
        try {
          data = await key.bundle.load(key.name);
        } on FlutterError {
          PaintingBinding.instance!.imageCache!.evict(key);
    
    
          rethrow;          // THIS IS THE HIGHLIGHTED LINE
    
        }
        // `key.bundle.load` has a non-nullable return type, but might be null when
        // running with weak checking, so we need to null check it anyway (and
        // ignore the warning that the null-handling logic is dead code).
        if (data == null) { // ignore: dead_code
          PaintingBinding.instance!.imageCache!.evict(key);
          throw StateError('Unable to read data');
        }
        return await decode(data.buffer.asUint8List());
      }
    }
    

    I don't know what this means, so any help would be really appreciated. This is the image (Scroll):

    (sorry for it being so big) If you need more details just comment :)

    • Wessel
      Wessel about 3 years
      Make sure you have correctly specified the filepath to your asset, and to include the file extension. Also do not forget to specify the assets folder in your pubspec.yaml. see flutter.dev/docs/development/ui/assets-and-images
    • Aman Verma
      Aman Verma about 3 years
      Add the extension for image i.e jpg/png etc , Also try posting the error that you see after you run with run without debugging mode in vscode