如何将Flutter的Future<dynamic>转换为Future<other_type>?

How to convert Flutter's Future<dynamic> to Future<other_type>?

过去,我使用 returns Future<Response> 的 http 客户端。我注意到我可以在 .then() 方法中 return 其他类型,最终类型结果将根据我在 then.return 中编辑的内容的类型而改变。

但是当我更改为使用 returns Future<dynamic> 的包时,returning .then() 中的不同类型不再改变最终结果类型.它一直导致 Future<dynamic>。但是我可以保证动态类型是Response类型。这是我尝试过的:

第一名:

Future<Response> get(String url) =>
    fetcher.get(url).then((response) => response as Response); // error: Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<Response>'

第二名:

Future<Response> get(String url) =>
    fetcher.get(url) as Future<Response>; // error: Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<Response>'

第三名:

Future<Response> get(String url) =>
    fetcher.get(url).then((response) { 
        final result = response as Response;
        return result;
    }); // error: Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<Response>'

过去:

Future<List<Model>> get(String url) =>
    client.get(url).then((response) => Model.fromJson(response.body)); // working.

我为此使用的包:https://pub.dev/packages/twitter_api#-example-tab-

编辑:澄清获取器:

import 'package:twitter_api/twitter_api.dart';
import "package:http/http.dart";


twitterApi fetcher = twitterApi(
    consumerKey: consumerApiKey,
    consumerSecret: consumerApiSecretKey,
    token: locker.session?.token,
    tokenSecret: locker.session?.secret
);

Future<Response> get(String path) {
    final split = url.split("?");
    final onlyPath = split[0];
    final onlyParam = split.length > 1 ? split[1] : "";
    final options = Map.fromIterable(onlyParam.split("&").where((it) => it.contains("=")), key: (e) => e.split("=")[0] as String, value: (e) => e.split("=")[1] as String);
    return fetcher.getTwitterRequest("GET", onlyPath.replaceFirst("/", "", options: options).then((response) {
        debugPrint ("GET ${response.request.url} Response: ${response.body}");
        return response as Response;
    }); 
}

输出:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<Response>'
#0      TwitterApiProvider.fetchUser (package:feazy/src/resources/twitter_api_provider.dart:11:70)
#1      Repository.fetchUser (package:feazy/src/resources/repository.dart:19:27)
#2      _HomeState.twitterLogin (package:feazy/src/ui/home.dart:50:21)
...
GET https://api.twitter.com/1.1/users/show.json?screen_name=somescreenname Response: {"id":<int>,"id_str":<string>,"name":"Some name","screen_name":"somescreenname",...

编辑 2:进一步检查,我注意到 IDE 上的 IDE 告诉我 fetcher.getTwitterRequest() 的 return 类型不是 Future<dynamic>,但 dynamic。所以我像这样更改我的代码,但它仍然无法正常工作。

Future<Response> get (String path) {
    ...
    final future = authClient.getTwitterRequest(...).then((response) {
        debugPrint ("POST ${response.request.url} Response: ${response.body}");
        return response as Response;
    });
    final futureResponse = future as Future<Response>;
    return futureResponse;
}

在这种情况下,错误在final futureResponse = future as Future<Response>;,说同样的话:Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<Response>' in type cast

恕我直言,你所有的例子都没有问题,但错误实际上告诉我们你试图将 Future<Response> 转换为 Future<dynamic>

更新

您可以使用 async await,像这样:

Future<Response> get(String path) async {
    final split = url.split("?");
    final onlyPath = split[0];
    final onlyParam = split.length > 1 ? split[1] : "";
    final options = Map.fromIterable(onlyParam.split("&").where((it) => it.contains("=")), key: (e) => e.split("=")[0] as String, value: (e) => e.split("=")[1] as String);
    final response = await fetcher.getTwitterRequest("GET", onlyPath.replaceFirst("/", "", options: options); 
    return response as Response;
}