Last updated:
0 purchases
error handler
Welcome to ErrorHandler, error handler with type-safety/streaming/freezed-functionality/cover-all-clients-exceptions
Index #
Motivation
Functionality
How to use
install
Example
.future get api result directly full example
.stream provide Loading and Idle State full example
Advance login example for post request full example
Other Example
Contribute
Credits 🙏
Motivation #
try{}catch(e){} or then((){}).catch((){}) make code hard to read and modify
Functionality #
handle all api possible state init/loading/data/error easily
logging the state states
built above freezed
**work with any http client like chopper,dio and more **
Before
After
How to use #
install #
For a Flutter project:
flutter pub add error_handler
flutter pub add dio
copied to clipboard
For a Dart project:
flutter pub add error_handler
flutter pub add dio
copied to clipboard
Example #
.future get api result directly full example #
import 'package:dio/dio.dart';
import 'package:error_handler/error_handler.dart';
FutureResponse<Post> getPost() async {
const path = "https://jsonplaceholder.typicode.com/posts/1";
final response = await Dio().get(path);
return response.convert(Post.fromJson);
}
/// wrap the api call with [ErrorHandler.future]
Future<void> main() async {
final state = await errorHandler.future(getPost);
state.whenOrNull(
data: (post, response) {
print("title: ${post.title}");
},
error: (error) {
print(getErrorMessage(error));
},
);
}
copied to clipboard
errorHandler.future((){...}) return safe data
.stream provide Loading and Idle State full example #
/// wrap the api call with [ErrorHandler.stream]
///
/// to handle loading state
void main() {
final event = errorHandler.stream(getPost);
event.listen((state) {
state.whenOrNull(
loading: () {
print("loading");
},
data: (post, response) {
print("title: ${post.title}");
},
error: (error) {
print(getErrorMessage(error));
},
);
});
}
copied to clipboard
errorHandler.stream((){...}) first return loading and then return data or error
Advance login example for post request full example #
/// First create API call
FutureResponse<User> login(String gmail, String password) async {
final body = {"gmail": gmail, "password": password};
final response = await Dio().post("http://your.domain.com/login", data: body);
return response.convert(User.fromJson);
}
/// Wrap it with [ErrorHandler.stream] or [ErrorHandler.future]
StreamState<User> safeLogin(String gmail, String password) =>
errorHandler.stream(() => login(gmail, password));
void main() {
final event = safeLogin("[email protected]", "password");
event.listen((event) {
event.whenOrNull(
loading: () {
print("please wait");
},
data: (data, response) {
print("login successfully");
print(data);
},
error: (exception) {
print(exception.defaultErrorMessage());
},
);
});
}
copied to clipboard
Other Example #
status code 401 unauthorized
status code 401 unauthorized advance example with ErrorFilter
Contribute #
please fork the repo and be part of maintainers team ❤️🔥
Credits 🙏 #
Freezed
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.