dart json.encode(data) can not accept other language

雨燕双飞 提交于 2020-06-25 21:08:05

问题


I am currently doing web development with dart. Implemented service with mockclient. However, the following error occurs. The implementation code below is an in memory web api service that inherits mockClient. The code that calls client.send () and returns the result.

test_value is the result of json.encode (data).

var test_value = '{"id": 1, "type": "Appetizer", "name": "한글"}';

     return Response (test_value, 200, headers: {'content-type': 'application / json'});

ERROR

Invalid argument(s): String contains invalid characters.
dart:convert                                           Latin1Codec.encode
package:http/src/response.dart 36:49                   new Response
package:basil/common/mock_rest/mock_recipe.dart 40:12  MockRecipe._handler

If you put an English string in the name of the above implementation code, there is no error. Why do I get an error when I insert a character other than English?

Please let me know if you know!

A dart programmer struggling alone in Korea


回答1:


The Response class uses Latin-1 encoding for the body unless something else is specified. This is not documented clearly on the constructor itself, but the documentation on the body getter does suggest this.

Try setting the charset/encoding in the header, e.g., as:

return Response(test_value, 200, headers: {
    HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8'
});


来源:https://stackoverflow.com/questions/52990816/dart-json-encodedata-can-not-accept-other-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!