1

My code is :

var header = {"cookie": db.getString("cookie")};
var body={"nonce":_nonce.nonce,"action":"GetTrafficList" ,"params":{"filters":{"credit":{"mode":"none"}}}};
var response = await post(Urls.traffics, headers: header,body:body);

The following error occurs when executing :

Restarted application in 4,619ms.
E/flutter (22004): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type '_InternalLinkedHashMap<String, Map<String, Map<String, String>>>' is not a subtype of type 'String' in type cast
E/flutter (22004): #0      CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:288:25)
E/flutter (22004): #1      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (22004): #2      CastMap.forEach (dart:_internal/cast.dart:287:13)
E/flutter (22004): #3      mapToQuery (package:http/src/utils.dart:17:7)
E/flutter (22004): #4      Request.bodyFields= (package:http/src/request.dart:137:12)
E/flutter (22004): #5      BaseClient._sendUnstreamed (package:http/src/base_client.dart:170:17)
E/flutter (22004): #6      BaseClient.post (package:http/src/base_client.dart:58:7)
E/flutter (22004): #7      post.<anonymous closure> (package:http/http.dart:70:16)
E/flutter (22004): #8      _withClient (package:http/http.dart:166:20)
E/flutter (22004): #9      post (package:http/http.dart:69:5)
E/flutter (22004): #10     Traffic.GetTraffic (package:adsl_tci/Traffic.dart:23:26)
E/flutter (22004): <asynchronous suspension>

I think the problem is with the body content :

enter image description here

2 Answers 2

1

you have to mention about you'r request provider http or dio. hopefully i have some example in both

HTTP

Future<NetworkResponse> post(String url, {Map<String, dynamic> body}) async {
    var token = "AccessToken";
    return await http.post(_baseUrl + url,
        headers: {
          "Authorization": 'Bearer ' + token,
          "Accept": "application/json"
        },
        body: body);

}

as you can see we have to send endpoint url and body to this function. for creating a map of data you can create a function like this

Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['text'] = text;
    return data;
}

DIO

Dio getBaseDio() {
var token = "AccessToken";
Dio dio = Dio(BaseOptions(baseUrl: MPref.getString("ApiAddress"),

    headers: {
      "Authorization": "Bearer $token",
      "Accept": "application/json",
    }));


return dio;

}

Future<Response> post(String url, {Map<String, dynamic> body}) async {
    return await getBaseDio().post(url, data: FormData.fromMap(body));
});

}

I hope I was able to help.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to convert your body to json. Try encoding your body

var response = await post(Urls.traffics, headers: header, body: json.encode(body));

You will need to import 'dart:convert'; at the top of your file

2 Comments

What you mean by that? The body parameter of http.post() needs a String value, you are not providing a String value in your code
when var body={"nonce":_nonce.nonce,"action":"GetTrafficList" } not problem but when change to var body={"nonce":_nonce.nonce,"action":"GetTrafficList" ,"params":{"filters":{"credit":{"mode":"none"}}}} An error occurs

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.