Home > Enterprise >  Why does Dart complain about null safety in one function but not the other - functions are the same?
Why does Dart complain about null safety in one function but not the other - functions are the same?

Time:12-10

The following code executes perfectly:

  @override
  State<NewTransaction> createState() => _NewTransactionState();

This code, however, does not. Error is: The body might complete normally, causing 'null' to be returned...

  @override
  State<NewTransaction> createState() {
    _NewTransactionState();
  }

Why? Isn't this just two alternative ways of writing the same thing?

(Latest Android Studio if that is important).

CodePudding user response:

The second function is missing a return statement.

  @override
  State<NewTransaction> createState() {
   return _NewTransactionState();
  }

CodePudding user response:

The second one must return the state like this:

  @override
  State<NewTransaction> createState() {
    return _NewTransactionState();
  }
  • Related