类型'List< Map< String,String>>'在类型转换中不是'String'类型的子类型 [英] type 'List<Map<String, String>>' is not a subtype of type 'String' in type cast

查看:104
本文介绍了类型'List< Map< String,String>>'在类型转换中不是'String'类型的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目上工作,并且在发布请求时遇到错误,当我使用邮递员时,它工作正常,但是当我使用Flutter时,它给了我异常,说type 'List<Map<String, String>>' is not a subtype of type 'String' in type cast

I work on a project and faced error when I post a request, When I use postman it works fine, but when I use Flutter it gives me exception said type 'List<Map<String, String>>' is not a subtype of type 'String' in type cast

  • 请求使用嵌套列表.

邮递员的身体和反应: ( https://i.imgur.com/HS5Y6CA.png )

Postman body and response : (https://i.imgur.com/HS5Y6CA.png)

Flutter代码:

Flutter Code :

try {
    http.Response response = await http.post(Uri.parse(url),
        headers: {
            "Accept": "application/json",
            'authorization' : 'Bearer ${tokenValue}',
        },
        body: {
            "date": "2019-12-30",
            "studentId": "1",
            "amount": "10",
            "numberOfItems": "2",
            "mqsfId": "1",
            "items": [{
                "itemCount": "1",
                "productId": "1",
                "productName": "Apple juice",
                "productPrice": "8"
            }, {
                "itemCount": "1",
                "productId": "2",
                "productName": "Sandwish",
                "productPrice": "2"
            }]
    });

    resBody = json.decode(response.body);
    if (response.statusCode == 201) {
        // Some Actions
    } else {
        // Some Actions
    }
} catch(e) {
    print(e);
}

此代码返回我异常提示:type 'List<Map<String, String>>' is not a sub type of type 'String' in type cast

this code returns me exception said: type 'List<Map<String, String>>' is not a sub type of type 'String' in type cast

当我将嵌套列表转换为String时:

when I convert nested List to String like this:

"items":[{
    "itemCount": "1",
    "productId": "1",
    "productName": "Apple juice",
    "productPrice": "8"
}, {
   "itemCount": "1",
   "productId": "2",
   "productName": "Sandwish",
   "productPrice": "2"
}].toString()

它发送请求并以StatusCode 500返回服务器错误,但是当我检查数据库时记录了订单,但是nested array is empty !!

It sends the request and returns Server Error with StatusCode 500, but when I check on the database the order is recorded but the nested array is empty !!

记录的数据示例(服务器错误请求的输出):

Example of data recorded (Output of server error request):

date: 2019-12-30,
studentId: 1,
amount: 10,
numberOfItems: 2,
mqsfId: 1,
items:[]
// item shouldn't be empty

推荐答案

直到我发现这个来自文档:

body sets the body of the request. It can be a String, a List<int> or a Map<String, String>.

并且仅将字段的值转换为String显然会阻止其解析.

And making only a field's value converted to String apparently prevents its parsing.

好,让我们尝试一下(基于此处):

Ok let's try this (based on here):

        http.Response response = await http.post(Uri.parse(url),
            headers: {
              'Content-type' : 'application/json',
              "Accept": "application/json",
              'authorization' : 'Bearer ${tokenValue}',
            },
            body: json.encode({
              "date": "2019-12-30",
              "studentId": "1",
              "amount": "10",
              "numberOfItems": "2",
              "mqsfId": "1",
              "items":[
                {
                  "itemCount": "1",
                  "productId": "1",
                  "productName": "Apple juice",
                  "productPrice": "8"
                },
                {
                  "itemCount": "1",
                  "productId": "2",
                  "productName": "Sandwish",
                  "productPrice": "2"
                }
              ]
            })
        );

这篇关于类型'List&lt; Map&lt; String,String&gt;&gt;'在类型转换中不是'String'类型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆