error_handler 1.0.6 error_handler: ^1.0.6 copied to clipboard
error handler for all http client in dart like dio, chopper http and more
Welcome to ErrorHandler, error handler with type-safety/streaming/freezed-functionality/cover-all-clients-exceptions
Index #
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 **
How to use #
install #
For a Flutter project:
flutter pub add error_handler
flutter pub add dio
For a Dart project:
flutter pub add error_handler
flutter pub add dio
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));
},
);
}
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));
},
);
});
}
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("example@domain.com", "password");
event.listen((event) {
event.whenOrNull(
loading: () {
print("please wait");
},
data: (data, response) {
print("login successfully");
print(data);
},
error: (exception) {
print(exception.defaultErrorMessage());
},
);
});
}
Contribute #
please fork the repo and be part of maintainers team ❤️🔥