Je ne parviens pas à obtenir l'image du dossier des éléments. exception show Fournisseur d'image: AssetImage (bundle: null, name: "assets / 1.jpg") s'il vous plaît, aidez-moi
/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): ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
4 Réponses :
Vous devez ajouter votre image à 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
J'ai créé un GitHub repo pour cela, veuillez suivre la même structure de fichiers, cela devrait fonctionner!
Montrez-moi la structure de vos dossiers!
# La section suivante est spécifique à Flutter. flutter: # La ligne suivante garantit que la police Material Icons est # incluse avec votre application, afin que vous puissiez utiliser les icônes # dans la classe Material Icons. uses-material-design: true # Pour ajouter des actifs à votre application, ajoutez une section actifs, comme ceci: assets: - assets / 1.jpg
J'ai créé un dépôt GitHub pour cela, veuillez suivre la même structure de fichiers, cela devrait fonctionner! github.com/Taym95/stackoverflow_respons
Merci, maintenant ça marche. car j'ai dupliqué votre fichier pubspec.yaml. # la classe d'icônes matérielles. uses-material-design: true # Pour ajouter des assets à votre application, ajoutez une section assets, comme ceci: assets: - assets / luchadortocat.1.png juste seulement, Soudain ça marche. Je ne sais pas pourquoi ça marche
vous devez mentionner les images dans pubspect.yaml pour les utiliser dans le projet, comme ci-dessous.
Image.asset('images/1.jpg')
alors vous pouvez utiliser une image comme celle-ci,
flutter: assets: - assets/1.jpg
Remarque: - assurez-vous que votre image se trouve dans le dossier nommé " images "
J'utilise child: GridTile (child: Image.asset ('assets / 1.jpg'),), Dans mon code
puis utilisez cet enfant: Image.asset ('images / 1.jpg'),
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'),
),
),
),),
);
}
}
Créez un dossier dans le répertoire principal du projet appelé assets, et à l'intérieur créez un dossier appelé images. Au dossier images, ajoutez l'image appelée 1.jpg. Dans votre pubspec.yaml:
Image.asset('assets/images/1.jpg')
ou pour ajouter tout le répertoire d'images:
# 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/
Dans votre code:
# 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
Vous devez ajouter les actifs dans le fichier pubsec.yml
ce lien peut aider flutter.dev/docs/development/ui/assets-and- images
oui, je l'ai fait mais ça ne marche pas @ dhuma191
Copie possible de Erreur des ressources Flutter: EXCEPTION PRISE PAR LE SERVICE DE RESSOURCE IMAGE < / a>