Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何在while循环中使用下载功能?_Loops_Flutter_While Loop - Fatal编程技术网

Loops 如何在while循环中使用下载功能?

Loops 如何在while循环中使用下载功能?,loops,flutter,while-loop,Loops,Flutter,While Loop,我有下面的代码,目标是在每个图像下面放置一个下载按钮,并在单击时下载特定的图像。然而,当我运行代码时,对于所有的按钮,它都试图下载9号图像,而循环运行仅为8个图像的I=8。请帮助如何解决此问题 Widget getWidgets(int index, String name) { List<Widget> list = new List<Widget>(); int i = 1; while (i <= index) { list.add(new

我有下面的代码,目标是在每个图像下面放置一个下载按钮,并在单击时下载特定的图像。然而,当我运行代码时,对于所有的按钮,它都试图下载9号图像,而循环运行仅为8个图像的I=8。请帮助如何解决此问题

Widget getWidgets(int index, String name) {
  List<Widget> list = new List<Widget>();
  int i = 1;
  while (i <= index) {
    list.add(new Column(
      children: <Widget>[
        SizedBox(
          height: 15.0,
        ),
        BeforeAfter(
          beforeImage: Image.asset(
            'assets/$name/OG$i.jpg',
            //fit: BoxFit.cover,
          ),
          afterImage: Image.asset(
            'assets/$name/$i.jpg',
            //fit: BoxFit.cover,
          ),
          isVertical: false,
        ),
        SizedBox(
          height: 10.0,
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("$name $i",
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 17,
                    fontFamily: 'Raleway',
                    color: Colors.black)),
            Container(
              margin: EdgeInsets.only(left: 100.0),
              width: 120.0,
              height: 35.0,
              child: FlatButton(
                onPressed: () async {
                  final status = await Permission.storage.request();
                  if (status.isGranted) {
                    final externalDir = await getExternalStorageDirectory();
                    FlutterDownloader.enqueue(
                      url: 'assets/$name/$i.dng',
                      savedDir: externalDir.path,
                      fileName: '$name$i.dng',
                      showNotification: true,
                      openFileFromNotification: true,
                    );
                  } else {
                    print("Permission Denied");
                  }
                },
                child: Text('Download',
                    style: TextStyle(color: Colors.black, fontSize: 17)),
                textColor: Colors.white,
                shape: RoundedRectangleBorder(
                    side: BorderSide(
                        color: Colors.teal, width: 2, style: BorderStyle.solid),
                    borderRadius: BorderRadius.circular(50)),
              ),
            ),
          ],
        ),
      ],
    ));
    i++;
  }
  return Column(children: list);
}
Widget getWidgets(int索引,字符串名称){
列表=新列表();
int i=1;

而(i实际上,在循环的末尾,
i
9
,这就是为什么您看到了自己的行为。您必须创建
i
的本地副本,以便在捕获该变量时,该变量保持其在循环中的值:

while (i <= index) {
   final localIndex = i;

   // rest of your code, use localIndex here instead of i

   i++;
}   

while(i实际上,在循环的末尾,
i
9
,这就是为什么您看到了自己的行为。您必须创建
i
的本地副本,以便在捕获该变量时,该变量保持在循环的值:

while (i <= index) {
   final localIndex = i;

   // rest of your code, use localIndex here instead of i

   i++;
}   

while(非常感谢!:)非常感谢!:)