Skip to content

Wrong run-time type of the value returned by generator function #53051

Closed
@sgrekhov

Description

@sgrekhov

Why run-time type of the variable from the below is Future?

import "dart:async";

FutureOr<Stream<int>?> f1() async* {
  yield 1;
  yield 2;
  yield 3;
}

main() async {
  Stream<int>? res1 = await f1();
  List<int>? it1 = await res1?.toList();
  print(it1 is List<int>?); // Analyzer: Unnecessary type check; the result is always 'true'.
                            // Prints `false`
  print(it1.runtimeType);   // Future<List<dynamic>>
}

Ok, the fact that type of it1 is List<dynamic> instead of List<int> is a known issue described here. But why it is Future?

Note that if to remove nullability issue connected with the Future disappers

import "dart:async";

FutureOr<Stream<int>> f1() async* {
  yield 1;
  yield 2;
  yield 3;
}

main() async {
  Stream<int> res1 = await f1();
  List<int> it1 = await res1.toList();
  print(it1 is List<int>); // Analyzer: Unnecessary type check; the result is always 'true'.
                            // Prints `false`
  print(it1.runtimeType);   // List<dynamic>
}

Tested on Dart SDK version: 3.1.0-333.0.dev (dev) (Thu Jul 20 13:04:10 2023 -0700) on "windows_x64"

cc @eernstg @lrhn

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-as-intendedClosed as the reported issue is expected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions