我在 try 块之间检查我的错误。如何通过创建中间层而不是在每个方法中重写它来摆脱代码重复?
如代码所示,我在所有操作中都使用了 try 块。
Future<List<Post>> getAll(){
try {
//http get code
}on SocketException {
throw Failure('Message');
}on HttpException {
throw Failure("Http Exception Message");
}on FormatException {
throw Failure("Format Message");
}
}
Future<Post> getPost(){
try {
//http get code
}on SocketException {
throw Failure('Message');
}on HttpException {
throw Failure("Http Exception Message");
}on FormatException {
throw Failure("Format Message");
}
}