Skip to content

Data Channel is a Dart utility for routing exceptions and data in Dart and Flutter, providing a streamlined alternative to conventional try and catch blocks.

License

Notifications You must be signed in to change notification settings

ganeshrvel/pub-data-channel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

Data Channel for Dart and Flutter.

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.

Installation

Go to https://pub.dev/packages/data_channel#-installing-tab- for the latest version of data_channel

Example

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();
    },
  );

Buy me a coffee

Help me keep the app FREE and open for all. Paypal me: paypal.me/ganeshrvel

Contacts

Please feel free to contact me at ganeshrvel@outlook.com

About

License

scaff | Scaffold Generator for Dart and Flutter. MIT License.

Copyright © 2018-Present Ganesh Rathinavel

About

Data Channel is a Dart utility for routing exceptions and data in Dart and Flutter, providing a streamlined alternative to conventional try and catch blocks.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published