http - flutter 错误 : type 'AddressInfo' is not a subtype of type 'String' in type cast

标签 http dart flutter

我制作了一个模型类并将其转发给一个 put api。但它显示错误

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

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

模型类

@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) ); 

关于http - flutter 错误 : type 'AddressInfo' is not a subtype of type 'String' in type cast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53991193/

相关文章:

dart - 全局设置和拆卸函数

flutter - 常量列表文字中的值必须是常量

flutter - 在 Flutter 中使用 ListView 或 CustomScrollView 在顶部添加新元素时如何保持滚动位置

python - Flask 在 Chrome 浏览器访问该网站后不处理其他 HTTP 请求

使用 fiddler 调试 localhost

flutter - 如何在 Flutter 中创建 Toast

sqlite - Flutter Sqflite 多表模型

http - 如何在 Golang 中为 http.Get() 请求设置超时?

c# - 从 NameValueCollection 读取结构

javascript - 类型错误 : Cannot read property 'instanceIdentifier' of undefined (FirebaseStorage)