Home > database >  In Dart, when I delay the exec of a method, the value of its parameter is the one when the delayed c
In Dart, when I delay the exec of a method, the value of its parameter is the one when the delayed c

Time:08-25

//Time 0

Future.delayed(Duration(seconds:10),() => _someMethod(variableX));

//...other stuff

_someMethod() is called with the value of variableX at time 0 or with the value of variableX at time 10?

CodePudding user response:

The variableX variable reference is evaluated when the _someMethod(variableX) code is run, which happens in ten seconds.

If you want to keep the current value of a mutable variable, you need to keep it in a separate variable: ``dart var currentX = variableX; Future.delayed(Duration(seconds: 10), () => _someMethod(currentX));

  •  Tags:  
  • dart
  • Related