2

I've created a flutter package and images are inside images folder.

flutter:
  uses-material-design: true
  # To add assets to your package, add an assets section, like this:
  # assets:
  assets:
    - images/
    - images/location_pointer.png

I'm trying to load this image:

Widget build(BuildContext context) {
    return Scaffold(
        body: Image.asset(
          'images/location_pointer.png',
          width: 22.0,
          height: 44.0,
          fit: BoxFit.fill,
        ));
}

I've created a project and importing a package inside it:

dependencies:
  flutter:
    sdk: flutter
  abc_pkg:
    path: /Users/mosh/Documents/flutter proj/abcPackage/abc_pkg

I'm able to load the package when run this project but not able to load the images inside the package.

======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/location_pointer.png

When the exception was thrown, this was the stack: 
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "images/location_pointer.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#092c6(), name: "images/location_pointer.png", scale: 1.0)
====================================================================================================
4
  • Try to refer my answer here hope its help to you. Commented Mar 17, 2022 at 5:39
  • 1
    Can you try adding the package name in the Image.asset for example: Image.asset('icons/heart.png', package: 'my_icons') Commented Mar 17, 2022 at 5:40
  • tried that too. Nothing is working. Commented Mar 17, 2022 at 5:42
  • Including package name work for me. Of course you will also need to add assets in pubspec.yaml Image.asset('icons/heart.png', package: 'my_icons') Commented Dec 2, 2022 at 16:00

3 Answers 3

1

If you are sure that you add assets truly in the pubspec.yaml file just add the name of your package to the package field of image.asset like bellow:

Image.asset(
            e.flagUri!,
            width: 40,
            fit: BoxFit.fitWidth,
            package: 'igmu_package',// this is pacakge name
          )
Sign up to request clarification or add additional context in comments.

1 Comment

This works for me, thanks
0
  1. Make sure all your images are saved inside a directory called 'images', which is inside a directory called 'assets'.

  2. Update your pubspec.yaml as follows:

flutter:
  uses-material-design: true
  # To add assets to your package, add an assets section, like this:
  # assets:
  assets:
    - assets/images/location_pointer.png
    

  1. Update your dart file as follows:

Widget build(BuildContext context) {
    return Scaffold(
        body: Image.asset(
          'assets/images/location_pointer.png',
          width: 22.0,
          height: 44.0,
          fit: BoxFit.fill,
        ));
}

3 Comments

Still same error.
where you make folder of images. I mean in which directory @mohsin
its in main dir. assets/images
0

please try this code. and make an image folder in the same directory where you have the lib folder

in your pubspec.yaml file

 flutter:
    
   uses-material-design: true
    
   assets:
      - images/

and in your dart file

    Widget build(BuildContext context) {
    return Scaffold(
        body: Image.asset(
          'images/location_pointer.png',
          width: 22.0,
          height: 44.0,
          fit: BoxFit.fill,
        ));
}

1 Comment

Its in the same directory. Same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.