user19877113
user19877113

Reputation:

How can i show image in Flutter?

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

Upvotes: -1

Views: 1174

Answers (2)

eamirho3ein
eamirho3ein

Reputation: 17950

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.

Upvotes: 0

Yunus Emre &#199;elik
Yunus Emre &#199;elik

Reputation: 326

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

flutter:

  uses-material-design: true
  assets:
    - assets/logo.png

Upvotes: 2

Related Questions