FLUTTER ECOSYSTEM

tonio-ramirez/optional.dart

डार्ट के लिए ओप्शनल प्रकार का एक अमल

optional.dart प्रोजेक्ट कवर
Stars
39
Forks
7
अंतिम पुश (UTC)
12 अप्रैल 2022
प्रोजेक्ट स्थिति
सक्रिय
Juan Antonio Ramírez GitHub avatar
GITHUB User

Juan Antonio Ramírez ↗

भाषाएँDart

इस रिपॉज़िटरी के पैकेज

उपयोग की गई निर्भरताएँ

निर्भरता सूची 6 आइटम
  • collection^1.15.0
  • mockitoडेवलपमेंट>=5.0.0 <6.0.0
  • testडेवलपमेंट^1.16.5
  • pedanticडेवलपमेंट^1.11.0
  • build_runnerडेवलपमेंट>=1.10.0 <2.0.0
  • coverageडेवलपमेंट^1.0.3

मूल README

यह अंग्रेज़ी मूल स्नैपशॉट है। नवीनतम सामग्री GitHub पर देखें।

README खोलें / बंद करें

https://github.com/tonio-ramirez/optional.dart/workflows/tests/badge.svg?branch=master Coverage Status

Optional.dart

Optional.dart is an implementation of the Optional type, inspired by Java 8's Optional class.

Optional helps avoid null reference errors by wrapping values in an object that holds information regarding whether the value is present or not.

For example, when applying a set of operations to a value, any of which might return null, you can get rid of the null checks by using Optional.map():

String? getString() {
  // ..
  if (condition) {
    return null;
  } else {
    return "12";
  }
}

int? calculateSomething(String str) {
  // ...
  if (someCondition) {
    return null;
  } else {
    return 3 + int.parse(str);
  }
} 

double? calculateSomethingElse(int val) {
  // ...
  if (anotherCondition) {
    return null;
  } else {
    return val * 1.42;
  }
}

void main() {
  
  // before
  
  String? str;
  int? i;
  double? d;
  
  str = getString();
  
  if (str != null) {
    i = calculateSomething(str);
  }
  
  if (i != null) {
    d = calculateSomethingElse(i);
  }
  
  if (d != null) {
    print(d);
  }
  
  // with Optional
  
  Optional.ofNullable(getString())
    .map(calculateSomething)
    .map(calculateSomethingElse)
    .ifPresent(print);
}