我无法从Assets文件夹获取图像。异常显示图像提供者:AssetImage(捆绑:null,名称:“ assets / 1.jpg”) 请帮我
/flutter ( 7667): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter ( 7667): The following assertion was thrown resolving an image codec:
I/flutter ( 7667): Unable to load asset: assets/1.jpg
I/flutter ( 7667): When the exception was thrown, this was the stack:
I/flutter ( 7667): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter ( 7667): <asynchronous suspension>
I/flutter ( 7667): #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:464:44)
I/flutter ( 7667): <asynchronous suspension>
I/flutter ( 7667): #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:449:14)
I/flutter ( 7667): #3 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:315:48)
I/flutter ( 7667): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:157:22)
I/flutter ( 7667): #5 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:315:25)
I/flutter ( 7667): (elided 13 frames from package dart:async)
I/flutter ( 7667): Image provider: AssetImage(bundle: null, name: "assets/1.jpg")
I/flutter ( 7667): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#88414(), name: "assets/1.jpg", scale:
I/flutter ( 7667): 1.0)
I/flutter ( 7667): ════════════════════════════════════════════════════════════════════════════════════════════════════
答案 0 :(得分:0)
您需要将图像添加到pubspec.yaml
:
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- images
为此我创建了一个GitHub repo,请使用相同的文件结构,应该可以!
答案 1 :(得分:0)
您必须在 pubspect.yaml 中提及要在项目中使用的图像,如下所示。
flutter:
assets:
- assets/1.jpg
然后您可以使用这样的图像
Image.asset('images/1.jpg')
注意:-,请确保您在名为“ 图像”的文件夹中的图像
答案 2 :(得分:0)
import 'package:flutter/material.dart';
import 'package:kwanjai_next/constants/kwanjai_color.dart';
void main() => runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'GridView',
home: ProjectTest(),
)); // MaterilApp
class ProjectTest extends StatefulWidget{
@override
ProjectTestState createState() => new ProjectTestState();
}
class ProjectTestState extends State<ProjectTest>{
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
backgroundColor: KwanjaiColors.greenst,
title: Text('Project t'),
),
body: Container(
child: ProjectList(),
),
);
}
}
class ProjectList extends StatefulWidget{
@override
ProjectListState createState() => new ProjectListState();
}
class ProjectListState extends State<ProjectList>{
final list_item = [
{
"name" : "image 1",
"picture" : "assets/1.jpg",
"price" : 70,
"old_picture" : 90
},
{
"name" : "image 2",
"picture" : "assets/2.jpg",
"price" : 40,
"old_picture" : 30
},
{
"name" : "image 3",
"picture" : "assets/3.jpg",
"price" : 30,
"old_picture" : 100
},
{
"name" : "image 4",
"picture" : "assets/4.jpg",
"price" : 50,
"old_picture" : 90
},
{
"name" : "image 5",
"picture" : "assets/5.jpg",
"price" : 40,
"old_picture" : 80
},
{
"name" : "image 6",
"picture" : "assets/6.jpg",
"price" : 10,
"old_picture" : 20
},
{
"name" : "image 7",
"picture" : "assets/1.jpg",
"price" : 70,
"old_picture" : 90
},
{
"name" : "image 8",
"picture" : "assets/2.jpg",
"price" : 40,
"old_picture" : 30
},
{
"name" : "image 9",
"picture" : "assets/3.jpg",
"price" : 30,
"old_picture" : 100
},
{
"name" : "image 10",
"picture" : "assets/4.jpg",
"price" : 50,
"old_picture" : 90
},
{
"name" : "image 11",
"picture" : "assets/5.jpg",
"price" : 40,
"old_picture" : 80
},
{
"name" : "image 12",
"picture" : "assets/6.jpg",
"price" : 10,
"old_picture" : 20
},
];
@override
Widget build(BuildContext context){
return GridView.builder(
itemCount: list_item.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index){
return ProjectDetail(
project_name: list_item[index]['name'],
project_picture: list_item[index]['picture'],
project_price: list_item[index]['price'],
project_old: list_item[index]['old_picture'],
);
}
);
}
}
class ProjectDetail extends StatelessWidget{
final project_name;
final project_picture;
final project_price;
final project_old;
ProjectDetail({this.project_name,this.project_picture,this.project_price,this.project_old});
@override
Widget build(BuildContext context){
return Card(
child: Hero(tag: project_name,
child: Material(
child: InkWell(
onTap: (){},
child: GridTile(
child: Image.asset('assets/1.jpg'),
),
),
),),
);
}
}
答案 3 :(得分:0)
在项目的主目录中创建称为资产的文件夹,并在内部创建名为图像的文件夹。在images文件夹中添加名为1.jpg的图像。 在您的pubspec.yaml中:
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/1.jpg
或添加整个图像目录:
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/
在您的代码中:
Image.asset('assets/images/1.jpg')
答案 4 :(得分:0)
这对我有用 只需卸载然后重新安装应用即可清除设备中应用路径中的空文件或损坏文件。