I would like to handle different errors that could happening in Dart. I am using try/catch but wondering how to determine between the different errors that can occur. For instance I have this error when there is no network connection:

PlatformException(Error 17020, FIRAuthErrorDomain, Network error (such as timeout, interrupted connection or unreachable host) has occurred.)

Whilst having this error when the username/password is incorrect:

PlatformException(Error 17009, FIRAuthErrorDomain, The password is invalid or the user does not have a password.)

I would like to take different actions depending on the error that occurs. What would be the best approach here? Update: Ended up using the below way!

import 'package:flutter/services.dart' show PlatformException;

try {
      //Something!

    } on PlatformException catch (e) {
      switch (e.code) {
        case "Error 17009":
          // handle
          break;
        case "Error 17020":
          // handle
          break;
        case "Error 17011":
          //handle
          break;
        default:
          throw new UnimplementedError(e.code);
      }
    }


Solution 1: Günter Zöchbauer

I'd use a try/catch and a switch/case:

import 'package:flutter/services.dart' show PlatformException;

try {
  ...
} on PlatformException catch(e) {
  switch(e.code) {
    case '17009':
      // handle
      break;
    case '17020':
      // handle
      break;
    default:
      throw new UnimplementedError(error.code);
  }
}