networkx

Last updated:

0 purchases

networkx Image
networkx Images
Add to Cart

Description:

networkx

Custom network error types for easy error handling.
Getting started #
Install the latest version from pub.dev.
dependencies:
networkx: ^0.2.1
copied to clipboard
Usage #
import 'package:networkx/networkx.dart';

void main() {
const networkError = NetworkError.api(ApiValidationError.emailAlreadyExists);

// `maybeWhen`
networkError.maybeWhen(
api: (error) {
if (error == ApiValidationError.emailAlreadyExists) {
print('Email already exists.');
} else if (error == ApiValidationError.phoneNumberAlreadyExists) {
print('Phone number already exists.');
}
},
orElse: () => print('Something went wrong.'),
);

// `match`
if (networkError.match((error) => error.isEmailAlreadyExists)) {
print('Email already exists');
}

// `cast`
final signUpError = networkError.cast(SignUpError.fromApiError);

// `name`
print(signUpError.name);
}

enum ApiValidationError {
emailAlreadyExists,
phoneNumberAlreadyExists;

bool get isEmailAlreadyExists => this == emailAlreadyExists;

bool get isPhoneNumberAlreadyExists => this == phoneNumberAlreadyExists;
}

enum SignUpError {
emailAlreadyExists,
phoneNumberAlreadyExists,
otherValidationError;

factory SignUpError.fromApiError(ApiValidationError error) {
if (error.isEmailAlreadyExists) {
return SignUpError.emailAlreadyExists;
} else if (error.isPhoneNumberAlreadyExists) {
return SignUpError.phoneNumberAlreadyExists;
} else {
return SignUpError.otherValidationError;
}
}
}

copied to clipboard

You can use it with any HTTP client library. For example, here, you can find an extension to the Dio library, so you don't have to write it from scratch. Or you can see an example project here for more detail and usage.
Maintainers #

Elias Andualem

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.