0

I have this code

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()}");
    });
  }
}

It gives this error

flutter: type '_ImmutableMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'

Even i tried with simple

  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;
    }

I gives same error.

1 Answer 1

1

You need to change params default value from const {} to const <String, dynamic>{}. your getRouter function should return Future<Router> instead of Router because you need the value of authToken, otherwise your function won't return anything.

There are some other modification I made to your code which you can see here:

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);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.