我已经在这个问题上工作了一段时间,但似乎无法弄清楚问题到底是什么。在Dart(2)中,json.encode()似乎并没有给我我想要的结果。
我传递的Map<String, dynamic>是这样的:
_data = <String, dynamic>{
'uid': newProfileCreationModel.accountID,
'displayName': newProfileCreationModel.profileName,
'greeting': newProfileCreationModel.profileBody ?? '',
'avatarSrc': newProfileCreationModel.avatarSrc,
'heroSrc': newProfileCreationModel.heroSrc,
'accountType': newProfileCreationModel.accountType,
'cityID': newProfileCreationModel.cityID,
'cityName': newProfileCreationModel.cityName,
'country': newProfileCreationModel.countryCode,
'state': newProfileCreationModel.stateAbv,
};
Run Code Online (Sandbox Code Playgroud)
并将其转换为JSON
final String _jsonData = json.encode(_data);
Run Code Online (Sandbox Code Playgroud)
然后,我通过以下方式将其发送到Google Cloud函数。
final Uri _uri = Uri.parse(functionUrl);
final String _jsonData = json.encode(_data);
final String _userToken = await AuthenticationService.getUserToken();
HttpClient client = new HttpClient();
HttpClientRequest request = await client.postUrl(_uri);
request.headers.set('Authorization', 'Bearer ' + _userToken);
request.headers.set('Content-Type', 'application/json; charset=utf-8');
print('Encoded: ' + _jsonData); …Run Code Online (Sandbox Code Playgroud)