Dart Duration isNegative
Syntax & Examples


Duration.isNegative property

The `isNegative` property in Dart returns whether this Duration is negative.


Syntax of Duration.isNegative

The syntax of Duration.isNegative property is:

 bool isNegative 

This isNegative property of Duration returns whether this Duration is negative.

Return Type

Duration.isNegative returns value of type bool.



✐ Examples

1 Negative Duration

In this example,

  1. We create a Duration object duration1 with -3 days.
  2. We use the isNegative property to check if the duration is negative.
  3. We then print the result to standard output.

Dart Program

void main() {
  Duration duration1 = Duration(days: -3);
  bool isNegative1 = duration1.isNegative;
  print('Duration1 is negative: $isNegative1');
}

Output

Duration1 is negative: true

2 Non-negative Duration

In this example,

  1. We create a Duration object duration2 with 30 seconds.
  2. We use the isNegative property to check if the duration is negative.
  3. We then print the result to standard output.

Dart Program

void main() {
  Duration duration2 = Duration(seconds: 30);
  bool isNegative2 = duration2.isNegative;
  print('Duration2 is negative: $isNegative2');
}

Output

Duration2 is negative: false

3 Negative Duration

In this example,

  1. We create a Duration object duration3 with -20 minutes.
  2. We use the isNegative property to check if the duration is negative.
  3. We then print the result to standard output.

Dart Program

void main() {
  Duration duration3 = Duration(minutes: -20);
  bool isNegative3 = duration3.isNegative;
  print('Duration3 is negative: $isNegative3');
}

Output

Duration3 is negative: true

Summary

In this Dart tutorial, we learned about isNegative property of Duration: the syntax and few working examples with output and detailed explanation for each example.