Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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
Variables Dart-如何将迭代器值传递到Dart中的回调中?_Variables_Dart_Flutter_Reference_Iterator - Fatal编程技术网

Variables Dart-如何将迭代器值传递到Dart中的回调中?

Variables Dart-如何将迭代器值传递到Dart中的回调中?,variables,dart,flutter,reference,iterator,Variables,Dart,Flutter,Reference,Iterator,我有一些飞镖/颤振代码,基本上是这样做的: for(int i = 0; i < dataArray.length; i++){ int curr = i; table.add( new DataRow( ... onSelectChanged: (bool selected){ DataRow toSwap = table[curr];

我有一些飞镖/颤振代码,基本上是这样做的:

for(int i = 0; i < dataArray.length; i++){
     int curr = i;
     table.add(
          new DataRow(
               ...
               onSelectChanged: (bool selected){
                    DataRow toSwap = table[curr]; 
                    /*This is how I "get" a row to select it, 
                    as there is no getter for the "checked" property*/
                    ...
               }
          )
     );
}
for(int i=0;i

我需要在这个回调中使用
curr
变量,但是在方法中调用它时,它会反映迭代器的最终值。在Dart中添加回调时,如何使用
curr
的值?

我假设您在发布代码之前修改了代码。。如图所示,您的代码可以工作。因此,为了解决原始问题,您只需为每次迭代创建一个新变量,我假设您的原始代码在循环外部定义了该变量

ie,由@jamesdlin展示:

//按预期工作
对于(int i=0;i<10;i+=1){
最终int电流=i;
functionList.add(()=>print('$i$current');//0,11,2。。。
}
//将更新当前版本
电流;
对于(int i=0;i<10;i+=1){
电流=i;
functionList.add(()=>print('$i$current');//0 9,1 9,2 9。。。
}
总之:可变状态是邪恶的;-)接受
final
,这样您就不会在循环之外创建变量了。

。你能帮我寄封信吗?