data_channel (DC) is a simple dart utility for handling exceptions and data routing.
It is not a very ideal solution to handle exceptions using try and catch at every function call, use data_channel
instead. data_channel
will take care of routing errors and data out of a method.
Go to https://pub.dev/packages/data_channel#-installing-tab- for the latest version of data_channel
Return error or data from getSomeLoginData
method
import 'package:data_channel/data_channel.dart';
Future<DC<Exception, LoginModel>> getSomeLoginData() async {
try {
return DC.data(
someData,
);
} on Exception {
return DC.error(
CacheException(),
);
}
}
Check for errors
void doSomething() async {
final value = await getSomeLoginData();
if (value.hasError) {
// do something
} else if (value.hasData) {
// do something
}
}
DC forward
Avoid redundant error checks. Easily convert an incoming data model to another one and forward it to the callee. DC.forward
will return the error in case it encounters with one else data will be returned.
Future<DC<Exception, UserModel>> checkSomethingAndReturn() {
final loginData = await getSomeLoginData();
return DC.forward(
loginData,
UserModel(id: loginData.data?.tokenId),
);
}
DC pick
final appData = await getSomeLoginData();
appData.pick(
onError: (error) {
if (error is CacheException) {
alerts.setException(context, error);
}
},
onData: (data) {
value1 = data;
},
onNoData: () {
value1 = getDefaultValue();
},
);
// or
appData.pick(
onError: (error) {
if (error is CacheException) {
alerts.setException(context, error);
}
},
onNoError: (data) {
if (data != null) {
value1 = data;
return;
}
value1 = getDefaultValue();
},
);
Help me keep the app FREE and open for all. Paypal me: paypal.me/ganeshrvel
Please feel free to contact me at ganeshrvel@outlook.com
- Author: Ganesh Rathinavel
- License: MIT
- Package URL: https://pub.dev/packages/scaff
- Repo URL: https://github.com/ganeshrvel/pub-scaff
- Contacts: ganeshrvel@outlook.com
scaff | Scaffold Generator for Dart and Flutter. MIT License.
Copyright © 2018-Present Ganesh Rathinavel