Flutter错误:输入' AddressInfo'不是' String'类型的子类型.铸型 [英] Flutter Error: type 'AddressInfo' is not a subtype of type 'String' in type cast

查看:69
本文介绍了Flutter错误:输入' AddressInfo'不是' String'类型的子类型.铸型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个模型类,并将其转发给put api.但显示的错误是

I made a model class and forward it to a put api. but it shows the error that

type 'AddressInfo' is not a subtype of type 'String' in type cast

该类已正确序列化,我检查了地图的输出.此错误在http put请求期间发生

The class is serialized properly, I checked the output of the map.This error occurs during http put request

模型类

@JsonSerializable()
class AddressInfo{
  String city;
  List<String> operationAreas=new List();

  AddressInfo(
      this.city,
      this.operationAreas
      );

  factory AddressInfo.fromJson(Map<String, dynamic> json) => _$AddressInfoFromJson(json);
  Map<String, dynamic> toJson() => _$AddressInfoToJson(this);
}

API调用

Map input = addressinfo.toJson();
    String jsonData = json.encode(input);
    debugPrint("API CALLING json" + jsonData);

    http.Response response = await
    http.put(Apis.SIGNUP,
        body: input ); // post api call

错误

 type 'AddressInfo' is not a subtype of type 'String' in type cast
E/flutter ( 8969): #0      CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:286:25)
E/flutter ( 8969): #1      __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.forEach (dart:collection/runtime/libcompact_hash.dart:370:8)
E/flutter ( 8969): #2      CastMap.forEach (dart:_internal/cast.dart:285:13)
E/flutter ( 8969): #3      mapToQuery (package:http/src/utils.dart:17:7)
E/flutter ( 8969): #4      Request.bodyFields= (package:http/src/request.dart:128:17)
E/flutter ( 8969): #5      BaseClient._sendUnstreamed (package:http/src/base_client.dart:163:17)
E/flutter ( 8969): <asynchronous suspension>
E/flutter ( 8969): #6      BaseClient.put (package:http/src/base_client.dart:76:7)
E/flutter ( 8969): #7      put.<anonymous closure> (package:http/http.dart:94:16)
E/flutter ( 8969): #8      _withClient (package:http/http.dart:166:20)
E/flutter ( 8969): <asynchronous suspension>
E/flutter ( 8969): #9      put (package:http/http.dart:93:5)
E/flutter ( 8969): #10     signup (package:careio_cross_platform/screens/PricingSelectionScreen.dart:175:5)

推荐答案

替换此:

http.put(Apis.SIGNUP,
    body: input ); 

由此:

http.put(Apis.SIGNUP,
    body: json.encode(input) ); 

这篇关于Flutter错误:输入&amp;#39; AddressInfo&#39;不是&amp;#39; String&amp;#39;类型的子类型.铸型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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