-1

I build a folder assets and added there my logo.png. But it shows unable to load asset: assets/logo.png . How can I fix this? Below you can find my code.

import 'package:flutter/material.dart';
import 'package:color/color.dart';


class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
         color: Colors.grey[900],
         child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            SizedBox(
              height: 100,
              width: 150,
              child: Image.asset('assets/logo.png'),
            )
          ],
         ),
          )
        ],
      ),
    );
  }
}

image

1
  • Where did you put the assets folder? Commented Sep 6, 2022 at 12:23

2 Answers 2

2

You need to add also pubspec.yaml file like this.

flutter:

  uses-material-design: true
  assets:
    - assets/logo.png
Sign up to request clarification or add additional context in comments.

11 Comments

Emre . I did it but still same problem
Sometimes you may need to delete and reinstall the application. or if you are using android studio you need to do invalidate caches and restart
Usually we use like this. We create images folder under assets folder. After we define like this. - assets/images/ After that i add all photos into images folder.
I did what you say but same problem. I built the folder images inside assets
Can you send a picture your pubspec.yaml file and assets folder please
|
0

first you need create a asset folder in your project root folder, like this:

enter image description here

then in your pubspec.yaml , under flutter: define it like this:

assets:
    - assets/images/

enter image description here

then run flutter pub get now if you call image, you can see it:

Image.asset('assets/images/test.jpeg'),

enter image description here

Note: if you still not getting image, please stop project and close ide, then open it again run flutter pub get then run then project.

4 Comments

I did it exactly as you but still same error. @eamirho3ein
please stop project and close ide, then open it again run flutter pub get then run then project.
ok @eamirho3ein
Glad to hear that, please if it was helpful to you, up vote and approve it so other people could find it easier.