Stopwatch class
A stopwatch which measures time while it's running.
A stopwatch is either running or stopped. It measures the elapsed time that passes while the stopwatch is running.
When a stopwatch is initially created, it is stopped and has measured no elapsed time.
The elapsed time can be accessed in various formats using elapsed, elapsedMilliseconds, elapsedMicroseconds or elapsedTicks.
The stopwatch is started by calling start.
Example:
final stopwatch = Stopwatch();
print(stopwatch.elapsedMilliseconds); // 0
print(stopwatch.isRunning); // false
stopwatch.start();
print(stopwatch.isRunning); // true
To stop or pause the stopwatch, use stop. Use start to continue again when only pausing temporarily.
stopwatch.stop();
print(stopwatch.isRunning); // false
Duration elapsed = stopwatch.elapsed;
await Future.delayed(const Duration(seconds: 1));
assert(stopwatch.elapsed == elapsed); // No measured time elapsed.
stopwatch.start(); // Continue measuring.
The reset method sets the elapsed time back to zero. It can be called whether the stopwatch is running or not, and doesn't change whether it's running.
// Do some work.
stopwatch.stop();
print(stopwatch.elapsedMilliseconds); // Likely > 0.
stopwatch.reset();
print(stopwatch.elapsedMilliseconds); // 0
Constructors
Properties
- elapsed → Duration
-
The elapsedTicks counter converted to a Duration.
no setter
- elapsedMicroseconds → int
-
The elapsedTicks counter converted to microseconds.
no setter
- elapsedMilliseconds → int
-
The elapsedTicks counter converted to milliseconds.
no setter
- elapsedTicks → int
-
The elapsed number of clock ticks since calling start while the
Stopwatch is running.
no setter
- frequency → int
-
Frequency of the elapsed counter in Hz.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isRunning → bool
-
Whether the Stopwatch is currently running.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reset(
) → void - Resets the elapsed count to zero.
-
start(
) → void - Starts the Stopwatch.
-
stop(
) → void - Stops the Stopwatch.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited