首页 > 解决方案 > 在颤动中发送多部分请求中的对象

问题描述

在这里,我试图在多部分请求中发送对象映射,但我的请求作为字符串发送,我需要发送对象映射而不是字符串。但 request .fields 想要我的字符串。

    "barberAddress":{
              "State":"test",
        "City":"test",
        "Street": "test",
        "location":{
            "lat":1.2,
            "lang":2.1
        }
     },

这是我的帖子方法

postFormDataBarber({
@required String endPoint,
File picB,barberName,BarberAddress barberAddress, }) async {
var url = Uri.parse('${BaseUrl.baseUrl}$endPoint');
var request = http.MultipartRequest("POST", url); 
request.fields["barberAddress"] = json.encode(barberAddress);// {"State":"aaa","City":"vvv","Street":"eeee","location":{"lat":null,"lang":null}}

request.files.add(picB);   
final data = await request.send();
return data;

}

这是我的模型,我想将此模型映射对象添加到 Mango 数据库

    class BarberAddress {
    BarberAddress({
    @required this.state,
    @required this.city,
    @required this.street,
    this.location,
    });
   String state;
   String city;
   String street;
   BarberLocation location;
   factory BarberAddress.fromJson(Map<String, dynamic> json) => 
   BarberAddress(
   state: json["State"],
   city: json["City"],
   street: json["Street"],
   location: BarberLocation.fromJson(json["location"]),
    );
    class BarberLocation {
    BarberLocation({
    @required this.lat,
    @required this.lang,
    });
    double lat;
    double lang;
    factory BarberLocation.fromJson(Map<String, dynamic> json) => 
    BarberLocation(
    lat: json["lat"].toDouble(),
    lang: json["lang"].toDouble(),
    );
    Map<String, dynamic> toJson() => {
    "lat": lat,
    "lang": lang,
    };
    }

标签: flutterhttpmultipartform-data

解决方案


推荐阅读