imageflutterdartassetsflutter-dependencies

Unable To Load Image in Flutter


I am new to Flutter. When i tried to load assets, i faced with an issue.

I checked pubspec.yaml file for identiation but i couldn't find any problem.

I checked the given path.

I also tried another images, even extension(.png,.jpg)

This is the Exception:

The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/pink_flower.jpg

When the exception was thrown, this was the stack: 
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:668:31)
#2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:651:14)
#3      ImageProvider.resolveStreamForKey.<anonymous closure> 
(package:flutter/src/painting/image_provider.dart:504:13)
...

This is .yaml file

name: flutterproject
description: VFL Lisesi uygulaması flutter ile.

publish_to: 'none'

version: 1.0.0+1

environment:
sdk: ">=2.7.0 <3.0.0"

dependencies:
flutter:
    sdk: flutter
cupertino_icons: ^1.0.0

dev_dependencies:
flutter_test:
    sdk: flutter
    
flutter:

uses-material-design: true

assets:
    - assets/images/flower2.png
    - assets/images/pink_flower.jpg

fonts:
    - family: Montserrat
    fonts:
        - asset: assets/fonts/montserrat_black.ttf
        weight: 300
        - asset: fonts/montserrat_black_italic.ttf
        weight: 300
        - asset: fonts/montserrat_bold.ttf
        weight: 600
    - family: Trajan Pro
    fonts:
        - asset: fonts/TrajanPro.ttf
        - asset: fonts/TrajanPro_Bold.ttf
        weight: 700

ImageWidget

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

class ImageBanner extends StatelessWidget {

final String _path;

ImageBanner(this._path);

@override
Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
    constraints: BoxConstraints.expand(
        height: 300.0,
    ),
    child: Image.asset(
        _path,
        fit: BoxFit.cover,
    ),
    );
}

}

calling ImageWidget

ImageWidget('assets/images/pink_flower.jpg')

Thanks for any helps!


Solution

  • Whenever you make changes to your pubspec.yaml file, make sure to perform these two steps afterwards.

    1. Run flutter pub get.

    2. Stop the ongoing session and do a full restart.