为T类调用ToJson方法



我是新来的Dart &颤振。我得把数据上传到网上。

我想使用一个通用的类方法。另一方面,我需要将我的对象转换为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.');
}
}
body: jsonEncode(((model) as dynamic).toJson())