Skip to content

Completer.complete() signature is not null-safe-friendly #1299

Open
@tvolkert

Description

@tvolkert

Consider the following definition of Completer.complete():

abstract class Completer<T> {
  void complete([FutureOr<T>? value]);
}

Now look at the following code:

void main() {
  Completer<int> completer = Completer<int>();
  completer.future.then((int value) {
    print(value);
  });
  completer.complete(null);
}

When running in sound null-safety, that code will produce no static analysis errors, compile cleanly, and throw a runtime error when it hits the call to completer.complete(null). This is highly counterintuitive and confusing to developers (at least it was to me when I ran into it).

Upon inspection, it's clear that this is clean to analysis because the completion value argument is optional. While this signature made sense in a pre-null-safety world, it's just going to be a source of errors when running with null safety.

I think we should consider a language change to fix this.

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