Dart argument test out of date?

In Dart 1.0.0, I just tried:

class MyClass {
    int x;
    bool b;

    MyClass(int x, [bool b = true]) {
        if(?b) {
            // ...
        }
    }
}

And I get a compiler error in part ?b:

Argument test (operator "?") Is deprecated

So, what is the “new” way of testing to provide an argument or not?

+4
source share
2 answers

There is no way to check if an argument is provided or not. The main reason for eliminating it was that it was very difficult to forward calls this way.

null " ". (, null ) . null, . null, :

foo([x = true, y]) => print("$x, $y");
foo();  // prints "true, null"

, , , :

class MyClass {
  int x;
  bool b;

  MyClass(int x, [bool b]) {
    if(b == null) {  // treat as if not given.
        // ...
    }
  }
}

new MyClass(5, null) new MyClass(5) . , :

class _Sentinel { const _Sentinel(); }
...
  MyClass(int x, [b = const _Sentinel()]) {
    if (b == const _Sentinel()) b = true;
    ...
  }

, . b.

+3

, null; , , null, null . == null:

class MyClass {
    int x;
    bool b;

    MyClass(int x, [bool b]) {
        if (b == null) {
            // throw exception or assign default value for b
        }
    }
}
+2

All Articles