How to catch exception in flutter?

前端 未结 3 609
别跟我提以往
别跟我提以往 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:58

    Future < User > userLogin(email, password) async { try {
      Map body = {
        'username': email,
        'password': password
      };
      http.Response response = await http.post(apiUrl, body: body);
      final responseBody = json.decode(response.body);
      final statusCode = response.statusCode;
      if (statusCode != HTTP_200_OK || responseBody == null) {
        throw new FetchDataException(
          "An error occured : [Status Code : $statusCode]");
       }
      return new User.fromMap(responseBody); }
       catch (e){
        print(e.toString());
    }
    

提交回复
热议问题