gpt4 book ai didi

flutter - Flutter Dart:错误_ImmutableMap <动态,动态>' is not a subtype of type ' Map

转载 作者:行者123 更新时间:2023-12-03 04:38:06 28 4
gpt4 key购买 nike

我有这个代码

import "./HTTPMethod.dart";
import '../../DataModel/DataModel.dart';
mixin RouterMixin {
HTTPMethod method;
String scheme;
String path;
String baseURL;
Map<String, dynamic> params;
Map<String, String> headers;
}
class Router with RouterMixin {
HTTPMethod method;
String scheme;
String path;
String baseURL;
Map<String, dynamic> params;
Map<String, String> headers;
Router._(this.method, this.path, this.scheme, this.baseURL, this.params,
this.headers);
// ignore: missing_return
static Router getRouter(method, path,
{scheme = "https://",
baseURL = "xyz.com",
params = const {},
headers = const {
"Accept": "application/json",
"Content-Type": "application/json"
}}) {
var headerValue = Map<String, dynamic>.from(headers);
DataModel.shared.authToken.then((value) {
print("TOKEN: $value");
if (value != null) {
headerValue["Authorization"] = value;
}
final router =
Router._(method, path, scheme, baseURL, params, headerValue);
return router;
}).catchError((error) {
print("ROUTER: ${error.toString()}");
});
}
}
它给出了这个错误
flutter: type '_ImmutableMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'
即使我尝试简单
  static Router routerValue(method, path,
{scheme = "https://",
baseURL = "zyx.com",
params = const {},
headers = const {
"Accept": "application/json",
"Content-Type": "application/json"
}}) {
Router router = Router._(method, path, scheme, baseURL, params,
{"Accept": "application/json", "Content-Type": "application/json"});
return router;
}
我给出了同样的错误。

最佳答案

您需要将params的默认值从const {}更改为const <String, dynamic>{}。您的getRouter函数应该返回Future<Router>而不是Router,因为您需要authToken的值,否则您的函数将不会返回任何内容。
我对您的代码进行了一些其他修改,您可以在此处看到:

abstract class RouterBase {
HTTPMethod method;
String scheme;
String path;
String baseURL;
Map<String, dynamic> params;
Map<String, dynamic> headers; // change to dynamic
}

class Router implements RouterBase {
HTTPMethod method;
String scheme;
String path;
String baseURL;
Map<String, dynamic> params;
Map<String, dynamic> headers;

Router._(this.method, this.path, this.scheme, this.baseURL, this.params,
this.headers);

static Future<Router> getRouter(
method,
path, {
scheme = "https://",
baseURL = "xyz.com",
params = const <String, dynamic>{}, // add generic types to fix your problem
headers = const {
"Accept": "application/json",
"Content-Type": "application/json"
},
}) async {
Map<String, dynamic> headerValue = HashMap<String, dynamic>.from(headers);
var authToken = await DataModel.shared.authToken;
if (authToken != null) {
headerValue["Authorization"] = authToken;
}
return Router._(method, path, scheme, baseURL, params, headerValue);
}
}

关于flutter - Flutter Dart:错误_ImmutableMap <动态,动态>' is not a subtype of type ' Map <String,动态>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63751768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com