supabase_result_handler 0.0.2
supabase_result_handler: ^0.0.2 copied to clipboard
A robust Result pattern wrapper for Supabase in Flutter. It handles Auth, Postgrest, and Functions exceptions with built-in English and Arabic localization support.
Supabase Result Handler #
A robust Flutter package to simplify error handling and result wrapping for Supabase applications. It leverages freezed to provide a type-safe Result pattern, automatically catching Supabase-specific exceptions (Auth, Postgrest, Functions) and converting them into user-friendly messages with multi-language support (English & Arabic).
Features 🚀 #
- Result Pattern: Wraps responses in
SuccessorFailuretypes. - Auto Error Handling: Automatically catches and categorizes Supabase errors (Auth, Database, Edge Functions).
- Localization Support: Built-in support for English and Arabic error messages.
- Type Safety: Built using
freezedto ensure compile-time safety. - Clean Syntax: Reduces boilerplate code with helper methods.
Installation 📦 #
Add this to your package's pubspec.yaml file:
dependencies:
supabase_result_handler: ^1.0.0
Usage #
Basic Usage #
Use SupaResult.catchError to automatically handle try-catch blocks and convert exceptions.
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:supabase_result_handler/supabase_result_handler.dart';
class AuthRepository {
final SupabaseClient _supabase = Supabase.instance.client;
// Return Future<SupaResult<T>>
Future<SupaResult<User>> login(String email, String password) async {
// Fast use with easy way to catch error
return SupaResult.catchError(() async {
final response = await _supabase.auth.signInWithPassword(
email: email,
password: password,
);
// Return the data you want on success
return response.user!;
});
}
}
Handling the Result in UI #
Use the .when method to switch between Success and Failure states easily.
void handleLogin() async {
final result = await authRepository.login('email@example.com', 'password');
result.when(
success: (user) {
print("User Logged in: ${user.id}");
// Navigate to Home
},
failure: (exception) {
// Get error message based on language
// Pass AppLanguage.ar for Arabic or AppLanguage.en for English
// The default language is Arabic
final errorMsg = exception.toErrorMessage(AppLanguage.ar);
print("Error: $errorMsg");
// Show SnackBar
},
);
}
Manual Usage (more customization) #
If you prefer to handle try-catch blocks manually, or need to do some complex logic in Catch:
try {
final data = await supabase.from('table').select();
return SupaResult.success(data: data);
} catch (e) {
// Convert generic error to NetworkExceptions
return SupaResult.failure(error: NetworkExceptions.getException(e));
}
Localization 🌍 #
The package supports English and Arabic out of the box.
// English
String enMessage = exception.toErrorMessage(AppLanguage.en);
// Arabic
String arMessage = exception.toErrorMessage(AppLanguage.ar);
Supported Exceptions 🛡️ #
The package handles a wide range of status codes and exceptions:
-
Auth Errors: 400, 401, 403, 422 (User already exists / Same password), etc.
-
Postgrest Errors: Database-related errors.
-
Edge Functions: Function execution errors.
-
Network: No Internet, Timeout, Socket Exceptions.
-
General: Format Exceptions, Unknown Errors.