FLUTTER: Unable to load asset: images/diamond.png

282 Views Asked by At

I know this question has been asked, I looked through all of them and couldn't find a fix for my code.

This is the pubspec yaml, I think that where I might have the error

name: i_am_rich
description: fuck this shit
version: 1.0.0+1

environment:
  sdk: '>=2.18.4 <3.0.0'

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter

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

and this is the dart

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.blueGrey,
        appBar: AppBar(
          centerTitle: true,
          title: Text('Shine bright like a'),
          backgroundColor: Colors.blueGrey[900],
        ),
        body: Center(
          child: Image(
            image: AssetImage('images/diamond.png'),
          ),
        ),
      ),
    ),
  );
}

I tried fixing the indentations on the pubspec.yaml, both manually and with tab, and a bunch of stuff from youtube videos, even the course I'm taking (not very good honestly), been trying to fix this for HOURS please help.

3

There are 3 best solutions below

1
On

do it this way

first in your pupspect yamel

# To add assets to your application, add an assets section, like this:
assets:
  - assets/images/
  - assets/logo/

then refer to it like this:

                          icon: Image.asset(
                            "assets/logo/google_logo.png",
                            fit: BoxFit.contain,
                            width: 24,
                          ),

dont forgot to create an assets folder like this

enter image description here

1
On

= Go to Terminal. and write these commands. "flutter clean " then " flutter pub get " sometimes flutter is like this. and make sure the image file is located in the root of project like the above person's answer and not inside lib.

0
On
Container(
      width: 150,
      height: 150,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(
            "images/diamond.png",
          ),
          fit: BoxFit.cover,
        ),
      ),
    ),