类型“String”无法分配给参数类型“Map<String, dynamic>”?尝试了一些解决方案,但仍然卡住

问题描述 投票:0回答:1

这是我的代码:

地图?我的地图 = { 'conversationId': iD, '类型':'文本', '数据': _controller.text.toString(), 'msgLength':_controller.text.length, 'senderId': Provider.of( context,listen: false).id, “创建于”:190823, 'messageState': '已送达', '已删除': 0, ‘id’:3048, };

          String encoded = json.encode(myMap);

     // Make the POST request and wait for the response
     final response = await postRequest(url: conversationUrl,
                                        body: encoded,
                                        headers:{'Content-Type': 'application/json'},);

但是我在主体参数中编码了一条红线,并显示错误:参数类型“String”无法分配给参数类型“Map?”

我已经尝试过(Dart HTTP POST with Map as body)更改,现在怎么办?

flutter http-post mobile-development
1个回答
0
投票

所以我认为可能发生的是 json 编码方式的问题,也许

我们通常这样做

body: jsonEncode(<String, String> {
        'key1':'value1', //hard coded
        'key2': 'value2',
        'key3': variable, //for variables given to the post function in the repository
        'boolean_key': booleanVar ? "True": "False", //for boolean variables
      }
      )

此外,json 编码的文档是这样说的:

Shorthand for json.encode. Useful if a local variable shadows the global json constant.
Example:
const data = {'text': 'foo', 'value': 2, 'status': false, 'extra': null};
final String jsonString = jsonEncode(data);
print(jsonString); // {"text":"foo","value":2,"status":false,"extra":null}

尝试将字符串编码为final

类型
© www.soinside.com 2019 - 2024. All rights reserved.