调用T类Tojson方法
我是Dart&扑。我必须将数据发布到Internet。
我将使用通用类方法为此。 另一方面,我需要将我的对象转换为JSON。 但是,如何通过T级课程或还有其他优雅的方式呢?
你能给我一个意见吗?
Future<ReturnModel> postData<T>(T model) async {
final response = await http.post(
Uri.parse('https://localhost/SomeAddress'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
//**
IDE gives error here of course.
Because it is not know type of T.
How can i get over this?
**//
body: jsonEncode(model.toJson())
);
if (response.statusCode == StatusCode.OK) {
return ReturnModel.fromJson(jsonDecode(response.body));
} else {
throw Exception('Bir hata oluştu.');
}
}
I'm new on Dart & Flutter. I have to post data to internet.
I would to use a common class method for this.
In other hand i need to convert my object to JSON.
But how can do it with T class or is there other elegant way?
Could you give me an opinion?
Future<ReturnModel> postData<T>(T model) async {
final response = await http.post(
Uri.parse('https://localhost/SomeAddress'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
//**
IDE gives error here of course.
Because it is not know type of T.
How can i get over this?
**//
body: jsonEncode(model.toJson())
);
if (response.statusCode == StatusCode.OK) {
return ReturnModel.fromJson(jsonDecode(response.body));
} else {
throw Exception('Bir hata oluştu.');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)