In Dart, syntactically nice way to cast dynamic to given type or return null? : <p>I have a <code>dynamic x</code> and I would like to assign <code>x</code> to <code>T s</code> if <code>x is T</code>, and otherwise assign <code>null</code> to <code>s</code>. Specifically, I would like to avoid having to type <code>x</code> twice, and to avoid creating a temporary. (For example, I don't want to have to write <code>String s = map['key'] is String ? map['key'] : null;</code> over and over, because I will have many such expressions.) I don't want there to be any possibility of a runtime error.</p>

<p>The following works:</p>

<pre><code>class Cast&lt;T&gt; {
  T f(x) {
    if (x is T) {
      return x;
    } else {
      return null;
    }
  }
}

// ...

dynamic x = something();
String s = Cast&lt;String&gt;().f(x);
</code></pre>

<p>Is there a syntactically nicer way to do this?</p>
