【问题标题】:type 'Position' is not a subtype of type 'String' (how to pass object in multipart request?)“Position”类型不是“String”类型的子类型(如何在多部分请求中传递对象?)
【发布时间】:2022-01-05 13:31:11
【问题描述】:

E/flutter (19556): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] 未处理的异常:“位置”类型不是“字符串”类型的子类型 位置是位置对象。

    var req = http.MultipartRequest('POST', uri);
    req.fields['category'] = _categoryTEC;
    req.fields['title'] = _titleTEC.text;
    req.fields['price'] = _priceTEC.text;
    req.fields['description'] = _descriptionTEC.text;
    req.fields['location'] = location;                   //position(lcation) is an object.
    req.headers.addAll(authHeadToken);
    req.files.add(await http.MultipartFile.fromPath(
        'adphoto', images!.first.path.toString()));
var res = await (req.send());```

【问题讨论】:

  • 好的,我发现了一个 hack,我将位置作为字符串发送 req.fields['location'] = jsonEncode(location); 因为 dart 在使用时将其转换为字符串jsonEncode(location) 并在后端解析为 JSON.parse(location); 以使其成为 json。 它对我有用虽然仍然需要答案并且将不胜感激。

标签: flutter http dart multipart


【解决方案1】:

“位置”是哪种类型的字段?如果需要字符串或双精度,则使用:

req.fields['location'] = location.latitude;
//or
req.fields['location'] = location.longitude;

【讨论】:

  • 位置是对象 { 经度:-122.084,纬度:37.421998333333335,时间戳:1641392725077,精度:5,高度:5,地板:空,航向:0,速度:0,speed_accuracy:0.5,is_mocked :假}
猜你喜欢
  • 2020-12-04
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 2021-02-05
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多