0

在 pubspec.yaml 文件上配置资产:

flutter:
  assets:
    - assets/product_images/

将图像保存在根过滤器上assets/product_images/ 并获取图像:

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);
}

错误:

════════ 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)
4

1 回答 1

0

试试这个,让我知道它是否有效。

static List<Product> getProducts() {
    products = List();
    products.add(Product("Heineken", 214.23, true, 'drink', Image.asset('assets/product_images/heineken_caixa.png')));
    return products;
}
于 2020-08-09T19:24:31.250 回答