skip to Main Content

I’ve been learning flutter for 2 months. I’m trying to develop a wallpaper app. I created a model and a function. But right now I can only download 1 wallpaper. How can I make this a list? I get this error when I make a list.

This is url.

  String url =
      'https://images.hdqwalls.com/download/the-witcher-season-2-2022-5k-u1-1080x1920.jpg';

the list i want to use

  List<String> url = [
    'https://images.hdqwalls.com/download/the-witcher-season-2-2022-5k-u1-1080x1920.jpg',
    'https://images.hdqwalls.com/download/the-witcher-season-2-2022-5k-u1-1080x1920.jpg',
  ];

and function

  void saveimage() async {
    await GallerySaver.saveImage(url, albumName: album_name);
  }

and clicking this button provides download

 ElevatedButton DownloadButton(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        minimumSize: Size(40, 40),
        shape: CircleBorder(),
        backgroundColor: Colors.grey.shade600.withOpacity(0.1),
      ),
      child: Icon(Icons.download, color: Colors.white.withOpacity(0.7)),
      onPressed: () {
        saveimage();
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            duration: Duration(seconds: 2),
            content: Text('Wallpaper downloaded!'),
            action: SnackBarAction(
              label: '',
              onPressed: () {},
            ),
          ),
        );
      },
    );
  }

Problem image

The packages I use are

  • gallery_saver: ^2.3.2
  • async_wallpaper: ^2.0.1

I want to use it in gridview

GridView.builder(
        itemCount: url.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: 9 / 16,
        ),
        itemBuilder: (BuildContext context, int index) {
          return Card(
            child: Padding(
              padding: EdgeInsets.all(1.0),
              child: FullScreenWidget(
                child: Stack(fit: StackFit.expand, children: [
                  Image.network(url, fit: BoxFit.cover),

2

Answers


  1.     void saveimage() async {
           for(int i = 0; i <= url.length; i++) {
                await GallerySaver.saveImage(url[I], albumName: album_name);
             }
          }
    

    Above code will resolve your error

    Login or Signup to reply.
  2. Let consider you want to download image when you click on the card then code will be like as following.

    void saveimage(String imageUrl) async {
        await GallerySaver.saveImage(imageUrl, albumName: album_name);
      }
    
    GridView.builder(
      itemCount: url.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2,
      childAspectRatio: 9 / 16,
      ),
      itemBuilder: (BuildContext context, int index) {
      return GestureDetector(
      onTap: (){
        await saveimage(url[index]);
      },
        child: Card(
        child: Padding(
        padding: EdgeInsets.all(1.0),
        child: FullScreenWidget(
        child: Stack(fit: StackFit.expand, children: [
        Image.network(url, fit: BoxFit.cover),]),),);),
      ),)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search