flutterdartmaterial-uiflutter-imageflutter-assetimage

Error to loading asset image to Image material on Flutter


Configuring assets on pubspec.yaml file:

flutter:
  assets:
    - assets/product_images/

save images on root folter assets/product_images/ and get Image:

import 'package:flutter/material.dart';
import 'package:/vo/product.dart';

class MockProducts  {

  static List<Product> products;

  static List<Product> getProducts() {
    products = List();
    products.add(Product("Heineken", 214.23, true, 'drink', Image.asset('product_images/heineken_caixa.png')));
    return products;
}

import 'package:flutter/material.dart';

class Product {

  String name;
  double price;
  bool available;
  String type;
  Image image;

  Product(this.name, this.price, this.available, this.type, this.image);
}

Error:

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: product_images/heineken_caixa.png

When the exception was thrown, this was the stack: 
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:664:31)
#2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:648:14)
#3      ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:501:13)
...
Image provider: AssetImage(bundle: null, name: "product_images/heineken_caixa.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#3dea9(), name: "product_images/heineken_caixa.png", scale: 1.0)

Solution

  • Try this, let me know if it works.

    static List<Product> getProducts() {
        products = List();
        products.add(Product("Heineken", 214.23, true, 'drink', Image.asset('assets/product_images/heineken_caixa.png')));
        return products;
    }