0
votes

I'm new in flutter, just started since last week. I'm learning from the online lesson, then i want to load a image in my flutter app. But there was an error which is :

════════ Exception caught by image resource service ════════════════════════════════════════════════ The following assertion was thrown resolving an image codec: Unable to load asset: assets/image2.jpg

When the exception was thrown, this was the stack: #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7) #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:670:14) Image provider: AssetImage(bundle: null, name: "assets/image2.jpg") Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#9f84b(), name: "assets/image2.jpg", scale: 1.0) ════════════════════════════════════════════════════════════════════════════════════════════════════

Below is the main.dart code :

  import 'package:flutter/material.dart';

void main() =>
  runApp(MaterialApp(
    home: Home(),
  ));

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar : AppBar(
        title : Text(
          "Welcome to HLH",
          style: TextStyle(
            fontSize: 20.0,
            color: Colors.black,
            fontFamily: "Goldman",
          ),
        ),
        centerTitle: false,
        backgroundColor: Colors.amber,
      ),
      body: Center(
        child: Image(
         image : AssetImage("assets/image2.jpg"),
        ),
      ),

      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Text("Login"),
      ),

    );
  }
}

Can anyone exists me how to solve it? I'm wondering is it the image problem? Thanks^^

2

2 Answers

0
votes

This can be a error of not declaring the assets in your pubspec.yaml file.

flutter: 
  assets:
    - images/

Add this to your pubspec.yaml file and then flutter pub get on the app root folder in terminal

0
votes

I think this problem was caused due to the image path!

Make sure that the path of the image correctly in pubspec.yaml (must be the same with the image path directory).

like this:-

i saved my image in this path "assets/images/cat.png"

Pubspec.yaml file :

flutter:
  assets:
    - assets/images/