i have the following
Dateime.now() here i need to increase only 1 microsecond
so wanted output is Dateime.now()
that incensement which is 1 microsecond
i tried the following but it does not work
print(DateTime.now() const Duration(microsecond : 1);)
How can i implement this
CodePudding user response:
This well get it done.
final now = DateTime.now();
final later = now.add(const Duration(millisecond: 1));
check docs here
Or do it in a single line:
DateTime now = DateTime.now().add(Duration(milliseconds: 1));
print(now);
CodePudding user response:
DateTime
does not define anoperator
, but it does have anadd
method that accepts aDuration
. (You also have a couple of syntax errors in your code; the semicolon is misplaced, and the named parameter toDuration
ismicroseconds
, notmicrosecond
.)If you're testing with Dart for the Web (such as with DartPad), you will not get microsecond precision due to limitations with JavaScript. Running the following code in the Dart VM will show a change in microseconds:
void main() { var now = DateTime.now(); const microsecond = Duration(microseconds: 1); print(now); // Prints: 2022-04-23 20:39:28.295803 print(now.add(microsecond)); // Prints: 2022-04-23 20:39:28.295804 }
Also see: https://stackoverflow.com/a/70848330/