Home > Back-end >  How to add network image in a List<ImageProvider>
How to add network image in a List<ImageProvider>

Time:11-10

I have a list of image.

final List<ImageProvider>_imageProviders_pro = [

       new Image.asset(
      "assets/page1.png",
      fit: BoxFit.fitWidth,
    ).image,


       new Image.asset(
      "assets/page2.png",
      fit: BoxFit.fitWidth,
    ).image,

...
];



@override
  Widget build(BuildContext context) {
    return Scaffold(
  body:
 ImageViewPager(imageProviders: _imageProviders_pro)
)
}

class ImageViewPager extends StatefulWidget {

  final List<ImageProvider> imageProviders;

  /// Create new instance, using the [imageProviders] to populate the [PageView]
  const ImageViewPager({ Key? key, required this.imageProviders }) : super(key: key);

  @override
  _ImageViewPagerState createState() => _ImageViewPagerState();
}

class _ImageViewPagerState extends State<ImageViewPager> {
 @override
  Widget build(BuildContext context) {
    return           PageView(
        children: <Widget>[
       
          PageView.builder(
            physics: _pagingEnabled ? const PageScrollPhysics() : const NeverScrollableScrollPhysics(),
            itemCount: widget.imageProviders.length,
            controller: _pageController,
            itemBuilder: (context, index) {
              final image = widget.imageProviders[index];
              return ImageView(
                imageProvider: image,
                onScaleChanged: (scale) {

                  setState(() {
                    _pagingEnabled = scale <= 1.0;

                  });
                },
              );
            },

       
          )

  }
}

Now, I try to use same list with Image.network. But I have this error The element type 'Image' can't be assigned to the list type 'ImageProvider'.

So I try to do something like thtat

final List<ImageProvider>_imageProviders_pro = [
         Image.network('url_page1')
         Image.network('url_page2')
];

CodePudding user response:

You can use .image to get image-provider from Image.network.

Image.networ("url").image
final List<ImageProvider> _imageProviders_pro = [
  Image.network('url_page1').image,
  Image.network('url_page2').image
];
  • Related