| 1 | import 'package:dio/dio.dart'; |
| 2 | import 'package:toastification/toastification.dart'; |
| 3 | import 'package:flutter/material.dart'; |
| 4 | |
| 5 | getErrorMessage(dynamic error, [String |
| 6 | if (error is String) { |
| 7 | return error; |
| 8 | } else if (error is Map |
| 9 | if (error['message'] is List |
| 10 | return error['message'][0]; |
| 11 | } else { |
| 12 | return error['message']; |
| 13 | } |
| 14 | } else { |
| 15 | return message ?? 'Something went wrong'; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | ToastificationItem showErrorToast(dynamic |
| 20 | final message = error is DioException |
| 21 | ? getErrorMessage(error.response? |
| 22 | : getErrorMessage(error, 'An unexpected error occurred' |
| 23 | return toastification.show( |
| 24 | title: const Text( |
| 25 | "An error occurred", |
| 26 | ), |
| 27 | description: Text(message), |
| 28 | type: ToastificationType.error, |
| 29 | style: ToastificationStyle.minimal, |
| 30 | autoCloseDuration: const Duration( |
| 31 | seconds: 3, |
| 32 | ), |
| 33 | showProgressBar: false, |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | ToastificationItem showSuccessToast({String |
| 38 | return toastification.show( |
| 39 | title: title != null |
| 40 | ? Text( |
| 41 | title, |
| 42 | ) |
| 43 | : null, |
| 44 | description: description != null ? Text |
| 45 | type: ToastificationType.success, |
| 46 | style: ToastificationStyle.minimal, |
| 47 | autoCloseDuration: const Duration( |
| 48 | seconds: 3, |
| 49 | ), |
| 50 | showProgressBar: false, |
| 51 | ); |
| 52 | } |