在 Flutter 上将资产图像加载到图像 material 时出错
Error to loading asset image to Image material on Flutter
在 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)
试试这个,如果有效请告诉我。
static List<Product> getProducts() {
products = List();
products.add(Product("Heineken", 214.23, true, 'drink', Image.asset('assets/product_images/heineken_caixa.png')));
return products;
}
在 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)
试试这个,如果有效请告诉我。
static List<Product> getProducts() {
products = List();
products.add(Product("Heineken", 214.23, true, 'drink', Image.asset('assets/product_images/heineken_caixa.png')));
return products;
}