简体   繁体   中英

How to add flutter image from local folder stored in assets

I have Stored local images in assets/images file added the pubspcs.yml also
assets: - Assets/images/ but i am fetching values from another dummy.dart file like this

return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(title: Text(categoryTitle)),
        body: ListView.builder(
          itemBuilder: (ctx, index) {
            return MealItem(
              id: displayedMeals[index].id,
              title: displayedMeals[index].title,
              **imageUrl: displayedMeals[index].imageUrl,**
              duration: displayedMeals[index].duration,
              affordability: displayedMeals[index].affordability,
              complexity: displayedMeals[index].complexity,
           // removeItem: _removeMeal,
            );
                /* Text(categoryMeals[index].title) */;
          },
          itemCount: displayedMeals.length,
        )
 );

and I want the image to come with this index file which i am fetching from dummy.dart

 imageUrl:
        'https://www.whiskaffair.com/wp-content/uploads/2018/02/Hyderabadi-Mutton-Biryani-6.jpg',

instead of this i want from the image from assets/images folder

You can use an asset image as default image.

 child: new Container(
      child: FadeInImage.assetNetwork(
          placeholder: 'place_holder.jpg',
          image:url
      )
  )

Put this in pubspec.yaml

  assets:
  - assets/place_holder.jpg

Images are resolved using ImageProvider .

There is a bunch of concrete implementations for different sources of images: FileImage , AssetImage , NetworkImage , MemoryImage , etc. Also there is associated factory constructors on Image class.

Probably you have something similar in MealItem :

Image.network(imageUrl)

You could either use ImageProvider :

Image(image: imageProvider)

Or directly provide Image widget in constructor.

Anyway you should know the source of the image to use appropriate ImageProvider implementation or Image constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM