Peary
Peary

Reputation: 23

I can't get image in flutter

I can't get image from assets folder . exception show Image provider: AssetImage(bundle: null, name: "assets/1.jpg") please, help me

/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): ════════════════════════════════════════════════════════════════════════════════════════════════════

Upvotes: 1

Views: 3768

Answers (5)

adel parsa
adel parsa

Reputation: 454

this worked for me Just uninstall then reinstall the app to clear empty or damaged file from app path in device.

Upvotes: 0

Taym95
Taym95

Reputation: 2510

You need to add you image to 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

I created a GitHub repo for this, please flow the same files structure, should work!

Upvotes: 0

Dawid Stefaniak
Dawid Stefaniak

Reputation: 346

Create folder in main directory of project called assets, and inside create folder called images. To images folder add the image called 1.jpg. In your 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

or to add whole directory of 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/

In your code:

 Image.asset('assets/images/1.jpg')

Upvotes: 1

Peary
Peary

Reputation: 23

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'),

           ),
         ),
       ),),
    );
  }
}

Upvotes: 0

Mayur Dabhi
Mayur Dabhi

Reputation: 3936

you have to mentioned images in pubspect.yaml for used in project, like below.

flutter:
  assets:
  - assets/1.jpg

then you can use image like this,

 Image.asset('images/1.jpg')

Note:- make sure your image in folder named "images"

Upvotes: 1

Related Questions