toastError static method

void toastError(
  1. dynamic error
)

Implementation

static void toastError(dynamic error) {
  var msg = '';

  if (error is Failure) {
    if (error is InternetFailure) {
      msg = 'Please check your internet';
    } else if (error is ErrorFailure) {
      msg = error.error;
    } else {
      msg = '$error';
    }
  } else if (error is String) {
    msg = error; // Directly assign if it's a string
  } else {
    msg = '$error';
  }

  // Handle special cases
  final errorValue = msg.contains('Connection closed before full header was received') ||
      msg.contains('FormatException: Unexpected character (at character 1)')
      ? 'Something went wrong! Please try again.'
      : msg.contains('Exception:')
      ? msg.replaceAll('Exception:', '').trim()
      : (msg.isEmpty ? 'An unknown error occurred' : msg);

  Fluttertoast.showToast(
    msg: errorValue,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIosWeb: 1,
    backgroundColor: Colors.red,
    textColor: Colors.white,
    fontSize: 16.0,
  );
}