[Breaking change request] StreamSubscription.cancel can no longer return null #40676
Labels
area-core-library
SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
breaking-change-request
This tracks requests for feedback on breaking changes
library-async
Milestone
Summary
The
StreamSubscription.cancel
method's return type is changed toFuture<void>
.What is changing:
We change the return type of
StreamSubscription.cancel
toFuture<void>
.The return type will be non-nullable in Null Safe code, and you are not allowed to return
null
.Why is this changing?
The
StreamSubscription.cancel
method currently has a return type ofFuture
(aka.Future<dynamic>
). It allows you to returnnull
as well, but this has been discouraged since the release of Dart 1, it's just not something the type system could prevent.When migrating this method to Null Safety, we have decided to change the return type to
Future<void>
(non-nullable). That means that implementations of theStreamSubscription
class can no longer return anull
value, and you can no longer use the value of the returned future without casting theFuture
away fromvoid
first.That is considered reasonable because the future is not intended to contain any useful value.
If a specific user of a specific stream subscription happens to know that that stream subscription's cancel returns a future with a useful value, then that should be reflected by having a sub-class of stream subscription with a different return type. (Or, as a hack, by casting the
Future<void>
toFuture<dynamic>
or similar before using it).Expected impact
The type change disallows code which currently awaits the returned future and uses its value.
That code needs to cast the future to something else first, so the result won't have type
void
.The non-null part won't be enforced until Null Safety, at which point tools will help find places where
null
is currently returned. That code should then returnFuture<Null>.value(null)
.The text was updated successfully, but these errors were encountered: