How to catch exception in flutter?

前端 未结 3 612
别跟我提以往
别跟我提以往 2020-12-31 01:06

This is my exception class. Exception class has been implemented by the abstract exception class of flutter. Am I missing something?

class FetchDataException          


        
3条回答
  •  一整个雨季
    2020-12-31 01:55

    Try

    void loginUser(String email, String password) async {
      try {
        var user = await _data
          .userLogin(email, password);
        _view.onLoginComplete(user);
          });
      } on FetchDataException catch(e) {
        print('error caught: $e');
        _view.onLoginError();
      }
    }
    

    catchError is sometimes a bit tricky to get right. With async/await you can use try/catch like with sync code and it is usually much easier to get right.

提交回复
热议问题