2

Trying to load image in flutter but not able to do that, getting following error

I/flutter ( 3214): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════ I/flutter ( 3214): The following assertion was thrown resolving an image codec: I/flutter ( 3214): Unable to load asset: assets/test.jpg I/flutter ( 3214): I/flutter ( 3214): When the exception was thrown, this was the stack: I/flutter ( 3214): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7) I/flutter ( 3214): I/flutter ( 3214): #1
AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:429:44) I/flutter ( 3214): I/flutter ( 3214): #2
AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:414:14) I/flutter ( 3214): #3 ImageProvider.resolve.. (package:flutter/src/painting/image_provider.dart:267:86) I/flutter ( 3214): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:143:20) I/flutter ( 3214): #5 ImageProvider.resolve. (package:flutter/src/painting/image_provider.dart:267:63) I/flutter ( 3214): (elided 8 frames from package dart:async) I/flutter ( 3214): I/flutter ( 3214): Image provider: AssetImage(bundle: null, name: "assets/test.jpg") I/flutter ( 3214): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#81de5(), name: "assets/test.jpg", scale: I/flutter ( 3214): 1.0) I/flutter ( 3214):

main.dart

lass MyApp extends StatelessWidget {
Widget build(BuildContext context) {
    return MaterialApp(
    home: Scaffold(
    appBar: AppBar(
      title: Text('EasyList'),
    ),
    body: Card(child: Column(children: < Widget > [
      Image.asset('assets/test.jpg'),
    ],),),
   ),
  );
 }
}

pubspec.yaml

flutter:
  uses-material-design: true

assets:
  - assets/test.jpg
mdDroid
  • 3,135
  • 2
  • 22
  • 34

2 Answers2

7

You need two spaces in front of the word assets: and two additional spaces in front of the dash (for a total of 4).

flutter:
  uses-material-design: true

  assets:
    - assets/test.jpg
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
4

Or if your are Using Android Studio IDE then in your pubspec.yaml file simply press CTRL+ALT+L to rearrange code . And then click packages get .

Hanish
  • 47
  • 7