Skip to content

Non null promotion fail in if( a != null ){...} #43764

@rxlabz

Description

@rxlabz

I maybe miss something but shouldn't the name property be promoted to NonNull in this example ? https://nullsafety.dartpad.dev/dcb8f17148eb07b1b7c36442eb2d53d7

In DartPad (Dart SDK 2.10.0), the analyzer feedback is : A value of type 'String?' can't be assigned to a variable of type 'String'.

class App {
  const App({this.name});

  final String? name;

  Map<String, dynamic> toJson() {
    final json = <String, String>{};

    if (name != null) {
      json['name'] = name;
    }
    return json;
  }
}

void main(){
  final app = App(name:'app');
  assert(app.toJson() == {"name":app});
}

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