Home > Mobile >  I want to duplicate image using a for loop in flutter
I want to duplicate image using a for loop in flutter

Time:01-16

I currently have no idea how to solve this problem. But I want to be able to duplicate the image based on the number sent to my method. here's what I have

dynamic _printImage(icon) { 
return Row(
  children: <Widget>[
    for (var i = 0; i < icon; i  ) {
      Image.asset(
        "assets/coin.svg",
      );
    }
  ],
); }

This is the error code I'm currently getting

Error Returned

CodePudding user response:

You have to add flutter_svg: version in pubspec.yaml and then

dependencies:
  flutter_svg: ^1.0.1

you can try :

  dynamic _printImage(icon) {
    return Row(
      children: List.generate(
        icon,
        (index) =>  SvgPicture.asset(
          "assets/coin.svg",
        ),
      ),
    );
  }

or :

  dynamic _printImage(icon) {
    return Row(children: <Widget>[
      for (var i = 0; i < icon; i  )
         SvgPicture.asset(
          "assets/coin.svg",
        )
    ]);
  }

CodePudding user response:

Avoid dynamic when possible.

The method provided by Cyrus is just fine, but Image Widget does not support SVG images. So, You can use: https://pub.dev/packages/flutter_svg

Widget _printImage(icon) {
    return Row(children: <Widget>[
      for (var i = 0; i < icon; i  )
        SvgPicture.asset(
          "assets/coin.svg",
        )
    ],
  );
}
  •  Tags:  
  • Related