1
votes

Exception caught by image resource service I am getting error while running the following code.

Code:

import 'package:flutter/material.dart';
void main(){
  runApp(MyApp());
}
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('PITCH'),
        ),
        body: Column(
          children: <Widget>[
            new Text('WELCOME'),
            new Image(image: AssetImage('download.png'))
          ],
        ),
      ),

    );
  }
}

Error:

flutter: ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════ flutter: The following assertion was thrown resolving an image codec: flutter: Unable to load asset: download.png flutter: flutter: When the exception was thrown, this was the stack: flutter: #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7) flutter: flutter: #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:464:44) flutter: flutter: #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:449:14) flutter: #3 ImageProvider.resolve... (package:flutter/src/painting/image_provider.dart:316:48) flutter: #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22) flutter: #5 ImageProvider.resolve.. (package:flutter/src/painting/image_provider.dart:316:25) flutter: (elided 13 frames from package dart:async) flutter: flutter: Image provider: AssetImage(bundle: null, name: "download.png") flutter: Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#ea200(), name: "download.png", scale: flutter: 1.0)

2
Make sure you stop the app process and then run the app again (full start)CopsOnRoad

2 Answers

3
votes

This might be because of following reasons.

  1. You forgot to add the asset in pubspec.yaml file

  2. You forgot to run flutter packages get command.

  3. You did a hot restart/reload of the app.

0
votes

Ensure these following things:

  • you've declared full path of the image in your pubspec.yaml file.
  • Run flutter pub get after declaring the asset images.
  • Write full path in the code just as you wrote in pubspec.yaml. So it should be 'assets/download.png'