Flutter 和 Dart 错误处理中间件概念

时间:2021-02-09 07:42:35

标签: flutter dart

我在 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");
  }
}

0 个答案:

没有答案