flutterpathassetsflutter-imagepubspec

My project don't work because of Flutter image


I have been trying to fix it, but I cannot add an image. Why is this?

enter image description here

I have trying to change path part but it didn't work.

maindart:

import 'package:flutter/material.dart';

import 'dashboard.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'my flutter app',
      themeMode: ThemeMode.system,
      debugShowCheckedModeBanner: false,
      home: DashBoard(),
    );
  }
}

dashboard:

import 'package:flutter/material.dart';

class DashBoard extends StatelessWidget {
  const DashBoard({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        body: Image.asset(
          "ghibli.jpg",
          width: 300,
          height: 400,
        ));
  }
}

Solution

  • add your image path to the pubspec.yaml file.

    flutter:
      uses-material-design: true
    
      assets:
        - images/ghibli.jpg
    

    then use it like:

    body: Image.asset(
        "images/ghibli.jpg",
         width: 300,
         height: 400,
      ));