Skip to content

Null safety: erroneous check with object members #1343

Closed
@DenisGL

Description

@DenisGL

Hello,
The issue is illustrated in this code snippet:

class Foo {
  String? _string;

  Foo() : _string = '';
  
  String doSomething() {
    return _string==null ? '' : _string; // => ERROR: A value of type String? cannot be returned...
  }
  
  String doSomething2(String? string) {
    return string==null ? '' : string;
  }
}

In doSomething(), the compiler issues an error: 'A value of type String? cannot be returned from method 'doSomething' because if has return type of String.

I don't understand why, because I have handled nullity with the test.

In addition, the same example is working if I get the input String? as parameter, and do not use the member (see doSomething2())

I have Dart 2.12.0-29.10.beta.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions