Skip to content

Type promotion doesn't work when a closure is used #1582

Open
@ghost

Description

@johnpryan commented on Apr 8, 2021, 12:12 AM UTC:

Result checkLottery(bool isLucky) {
  String? nullString;
  if (isLucky) {
    nullString = 'You win!';
  }

  if (nullString != null) {
    // Use this to fix:
    // var nonNullString = nullString;
    return () {
      return Result(nullString);
      //            ^ The argument type 'String?' can't be assigned to the parameter type 'String'.
    }();
  }

  return Result('You lose.');
}

@leafpetersen is there already an issue for this? I think this is called out in #44900

DartPad example

This issue was moved by keertip from dart-lang/sdk#45622.

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