实际上,我想在我的应用程序中添加图像,但是抛出了一个错误,说它无法加载图像资产。 这是我尝试过的几种方法: 1)在pubspec.yaml文件中保持资产的正确缩进,并且那里没有显示错误。 2)Ran-扑通干净,但仍然反复出现相同的错误。
here is screenshot of the error that I was thrown:
// pubspec.yaml: you can have a look at my pubspec.yaml file
//您可以检查我的代码。
` import 'package:flutter/material.dart';`
void main()=>runApp(MyApp());
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
`title:"flutter layout demo",`
` theme: ThemeData( `
primarySwatch: Colors.deepPurple,
`visualDensity: VisualDensity.adaptivePlatformDensity,`
),
home: HomePage(title:"flutter layout demo"),
);
}
}
class HomePage extends StatelessWidget{
HomePage({Key key,this.title}):super(key:key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:AppBar(
title: Text("ProductListing"),
),`
resizeToAvoidBottomPadding: false,
`body:ListView(`
`shrinkWrap: true,`
`padding: const EdgeInsets.fromLTRB(2, 10, 2, 10),`
`scrollDirection: Axis.vertical,`
`physics: AlwaysScrollableScrollPhysics(),`
`children: <Widget>[`
` ProductList(`
`name:"Iphone",`
`description:"Iphone is the most stylist phone you could ever get" ,`
`image: "images/iphone.jpg",`
`price: 200000,`
`),`
`ProductList(`
`name:"Pixel",`
`description:"Pixel is the most stylist phone you could ever get" ,`
`image: "images/pixel.jpg",`
`price: 200000,`
`),`
`ProductList(`
`name:"Tablet",`
`description:"Tablet is the most versatile and portable phone you could carry" ,`
`image: "images/tablet.jpg",`
`price: 50000,`
`),`
`ProductList(`
`name:"Laptop",`
`description:"Laptop is the most productive development tool" ,`
`image: "images/laptop.jpg",`
`price: 560000,`
`),`
`],`
` ),`
`);`
}
}
class ProductList extends StatelessWidget{
const ProductList({this.name,this.description,this.price,this.image});
final String name;
final String description;
final int price;
final String image;
@override
Widget build(BuildContext context) {
`return Container(`
` padding: const EdgeInsets.all(10),`
`height:120,`
`child:Card(`
`child: Row(`
` mainAxisAlignment: MainAxisAlignment.spaceEvenly,`
`children: <Widget>[`
` Image.asset("images/"+image),`
`Expanded(`
`child: Column(`
` mainAxisAlignment: MainAxisAlignment.spaceEvenly,`
`children: <Widget>[`
`Text(this.name,style: TextStyle(`
`fontWeight: FontWeight.bold),),`
`Text(this.description),`
`Text("price:" + this.price.toString()),`
`],`
`),`
`),`
`],`
`),`
`),`
`);`
}
}
`
答案 0 :(得分:1)
您已经在对象中定义了图像的路径。
image: "images/iphone.jpg",
因此,只需更改此内容即可:
Image.asset("images/"+image),
到
Image.asset(image),
答案 1 :(得分:0)
Image.asset('images/iphone.jpg')