Giter Site home page Giter Site logo

swatikulkarni123 / dartz_option_either Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 104 KB

This repository provides a Dart and Flutter library for handling API calls using Dartz's `Option` and `Either` types. It includes `BaseController` and `BaseView` classes to streamline common functionality in your Dart and Flutter projects.

Home Page: https://github.com/swatikulkarni123/Dartz_Option_Either

Kotlin 0.62% Swift 1.76% Objective-C 0.17% Dart 89.44% HTML 8.02%
android api apicall app clean-architecture dart dartz dio dynamic either

dartz_option_either's Introduction

Dartz Option and Either

GitHub stars

Dartz is a powerful functional programming library for Dart and Flutter. In this repository, we explore two fundamental concepts from Dartz: Option and Either. These concepts are particularly useful when making API calls or handling various outcomes in your Dart or Flutter projects.

Option

An Option is a type that represents a value that may or may not be present. It can have two possible values:

  1. none: This indicates that data is in the process of being fetched, or an API call is in progress.
  2. T: The T type can represent integers, strings, or any data type. In our case, T is an Either<ApiFailure, List<dynamic>>.

To access the value inside an Option variable, you need to use the fold method. The fold method enforces handling both conditions. You can assign a value to an Option using some(T).

Either

An Either is another powerful concept in Dartz. It can have two values:

  1. L: This represents a failure or error, typically ApiFailure in our case.
  2. R: This represents a success value, often List<dynamic> in our context.

Just like with Option, you use the fold method to access the values inside an Either variable.

BaseController and BaseView

In addition to Option and Either, we've included BaseController and BaseView in this repository. These components are designed to handle common functionality in your Dart and Flutter projects.

  • BaseController: This is a base controller class that provides common functionality for your controllers. It can help manage state, perform common operations, and simplify your app's logic.

  • BaseView: The BaseView is a base view class that offers a foundation for your UI components. It can help with layout, theming, and providing a consistent look and feel for your app.

Usage Example

Here's a brief example of how to use Option and Either:

Option<Either<ApiFailure, List<dynamic>>> apiResponse = none();

// Assign a value
apiResponse = some(left(ApiFailure)); // Represents an API failure
// OR
apiResponse = some(right(List<dynamic>)); // Represents API success

apiResponse.fold(
  () {
    // Handle the case when an API call is in progress
  },
  (eitherValue) => eitherValue.fold(
    (apiFailure) {
      // Handle API error
    },
    (success) {
      // Handle API success
    },
  ),
);

dartz_option_either's People

Contributors

swatikulkarni123 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.