Flutter:我想将每个 for 循环中的字符串值分配给 flutter 中的字符串列表,但出现无效范围错误

Flutter: I want to assign values of String from each for loop in to a list of Strings in flutter but getting error that invalid range

List<String> path = <String>[];
loopRun(int i) {
    for (int i = 0; i < 2; i++) {
      print("Called");
      
      _getData(
          files[i].toString(), i);
    }
  }
_getData(file, int i) async {    
    String thumb = file;
    print(thumb); 
    path[i] = thumb;
    print(path[i]);
    setState(() {      
    });
    return thumb;
  }

在上面的代码中,我调用了 loopRun(3) 并且循环 运行 三次,我在控制台中看到返回 3 个字符串的 thumb 值。我必须在路径中分配每个拇指字符串。为此,我声明了空字符串列表路径并尝试在路径 [i] 中分配但出现以下错误。

E/flutter (15093): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter (15093): #0      List._setIndexed (dart:core-patch/growable_array.dart:262:73)
E/flutter (15093): #1      List.[]= (dart:core-patch/growable_array.dart:258:5)
E/flutter (15093): #2      _MyHomePageState._getData
package:dtp22/main.dart:124
E/flutter (15093): <asynchronous suspension>
E/flutter (15093):

E/flutter (15093): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 1
E/flutter (15093): #0      List._setIndexed (dart:core-patch/growable_array.dart:262:73)
E/flutter (15093): #1      List.[]= (dart:core-patch/growable_array.dart:258:5)
E/flutter (15093): #2      _MyHomePageState._getData
package:dtp22/main.dart:124
E/flutter (15093): <asynchronous suspension>
E/flutter (15093):

如果你想在我认为你试图通过

实现的特定索引处插入一个元素
path[i] = thumb;

不是正确的方法,而是使用插入函数,对于您的情况,它看起来像

path.insert(i, thumb);
Map<int, String> path = new Map<int, String>();

List<String> files = <String>["test1","test2","test3"];

_getData(file, int i) async {    
    String thumb = file;
    path[i] = thumb;
    print(path[i]);
    return thumb;
  }
loopRun(int i) {
    for (int j = 0; j < i; j++) {
      print("Called");
      
      _getData(
          files[j].toString(), j);
    }
}