error: The argument type '(File) → Future<dynamic>' can't be assigned to the parameter type '(dynamic) → FutureOr<dynamic>'

error: The argument type '(File) → Future<dynamic>' can't be assigned to the parameter type '(dynamic) → FutureOr<dynamic>'

我正在尝试编译示例: https://github.com/dart-lang/googleapis_examples/blob/master/drive_upload_download_console/bin/main.dart

我得到以下 Dart 编译错误

error: The argument type '(File) → Future' can't be assigned to the parameter type '(dynamic) → FutureOr'. (argument_type_not_assignable at lib/google_api_rest/main.dart:49)

来自以下代码:

 // Download a file from Google Drive.
    Future downloadFile(drive.DriveApi api,
                        Client client,
                        String objectId,
                        String filename) {
      return api.files.get(objectId).then((drive.File file) {
        // The Drive API allows one to download files via `File.downloadUrl`.
        return client.readBytes(file.downloadUrl).then((bytes) {
          var stream = new File(filename).openWrite()..add(bytes);
          return stream.close();
        });
      });

我使用 Android Studio 3.2.1、Dart-Idk 2.1.0-dev.9.4 和 Flutter 1.0.0 稳定通道。

我是 Dart 和 Flutter 的新手, 有人可以帮我解决这个问题吗?

api.files.get() 静态声明为 return a Future. A bare Future with no generic is implicitly a Future<dynamic>. The Future<T>.then 调用需要一个 Function(T),这意味着 Function(dynamic)。您传递的是 Function(File).

您可能知道 Future 将始终解析为 File,但编译器静态地不知道这一点。

理想情况下,api.files.get() 的签名应更新为 returns Future<File>。与此同时,您可以解决这个问题:

  return api.files.get(objectId).then((result) {
    var file = result as drive.File;
    // The Drive API allows one to download files via `File.downloadUrl`.
    return client.readBytes(file.downloadUrl).then((bytes) {
      var stream = new File(filename).openWrite()..add(bytes);
      return stream.close();
    });