title: Dart null safety doesn't work with class fields
body: <p>I have migrated my Dart code to NNBD / Null Safety. Some of it looks like this:</p>
  <pre class="lang-dart prettyprint-override"><code>class Foo {
    String? _a;
    void foo() {
      if (_a != null) {
        _a += 'a';
      }
    }
  }
  
  class Bar {
    Bar() {
      _a = 'a';
    }
    String _a;
  }
  </code></pre>
  <p>This causes two analysis errors. For <code>_a += 'a';</code>:</p>
  <blockquote>
  <p>An expression whose value can be 'null' <strong>must be null-checked</strong> before it can be dereferenced.
  Try checking that the value isn't 'null' before dereferencing it.</p>
  </blockquote>
  <p>For <code>Bar() {</code>:</p>
  <blockquote>
  <p>Non-nullable instance field '_a' must be initialized.
  Try adding an initializer expression, <strong>or add a field initializer in this constructor</strong>, or mark it 'late'.</p>
  </blockquote>
  <p>In both cases I have already done exactly what the error suggests! What's up with that?</p>
  <p>I'm using Dart 2.12.0-133.2.beta (Tue Dec 15).</p>
  <p>Edit: I found <a href="https://dart.dev/null-safety" rel="noreferrer">this page</a> which says:</p>
  <blockquote>
  <p>The analyzer can’t model the flow of your whole application, so it can’t predict the values of global variables or class fields.</p>
  </blockquote>
  <p>But that doesn't make sense to me - there's only one possible flow control path from <code>if (_a != null)</code> to <code>_a += 'a';</code> in this case - there's no async code and Dart is single-threaded - so it doesn't matter that <code>_a</code> isn't local.</p>
  <p>And the error message for <code>Bar()</code> explicitly states the possibility of initialising the field in the constructor.</p>