पैकेज, रिपॉज़िटरी और ऐप श्रेणियाँ खोजें

पैकेज विवरणv1.0.0

result_type

डार्ट के लिए परिणाम प्रकार।

AndroidiOSWebmacOSWindowsLinux
30-दिन डाउनलोड
4.9 हज़ार
लाइक
35
Pub अंक
150

पैकेज इंस्टॉल करें

flutter pub add result_type

पैकेज अवलोकन

सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।

Result Type एक Dart पैकेज है जो सफलता के साथ मान या त्रुटि के साथ विफलता के साथ ऑपरेशन को हैंडल करने का एक बल्कि टाइप-सुरक्षित तरीका प्रदान करता है। कार्यात्मक प्रोग्रामिंग अवधारणाओं से प्रेरित, यह गणना के परिणामों का प्रतिनिधित्व करने के लिए `Result<T, E>` क्लास का परिचय देता है, जिससे त्रुटि हैंडलिंग अधिक स्पष्ट होती है और अपवादों की आवश्यकता कम होती है। यह चेनिंग, मैपिंग और त्रुटि प्रसारण का समर्थन करता है, जिससे कोड की पठनीयता और रखरखाव में सुधार होता है। व्यावसायिक तर्क और डेटा प्रसंस्करण वर्कफ्लो के लिए उपयुक्त जहां स्पष्ट सफलता/असफलता अवस्थाएँ आवश्यक होती हैं।

उपयोग

  • API प्रतिक्रियाओं का निपटान
  • उपयोगकर्ता इनपुट सत्यापन प्रसंस्करण
  • असिंक्रोनस संचालन का प्रबंधन
  • स्पष्ट त्रुटियों के साथ व्यवसाय तर्क का अनुप्रयोग
  • प्रयास-कैच ब्लॉक को संरचित प्रवाह के साथ प्रतिस्थापित करना

मुख्य विशेषताएँ

  • टाइप-सुरक्षित सफलता और त्रुटि प्रकार
  • चेनिंग और रूपांतरण विधियाँ
  • निर्मित त्रुटि प्रसारण
  • योज्य परिणाम निर्देशन
  • न्यूनतम बॉयलरप्लेट

इनके लिए उपयुक्त

  • Dart में फंक्शनल पैटर्न का उपयोग करने वाले डेवलपर्स
  • स्पष्ट त्रुटि संभाल की आवश्यकता वाले प्रोजेक्ट
  • पूर्वानुमानित अवस्था प्रबंधन के लिए टीमें
  • जटिल डेटा फ्लो वाले एप्लिकेशन
  • नियंत्रण प्रवाह के लिए अपवादों के विकल्प खोज रहे लोग

विचार योग्य बातें

  • यदि Result प्रकार से परिचित नहीं है, तो नई पैटर्न का अध्ययन करने की आवश्यकता होती है
  • सरल मामलों में विस्तार को बढ़ा सकता है
  • कम स्तर की प्रणाली त्रुटियों के लिए उपयुक्त नहीं
  • कुछ मौजूदा एक्सेप्शन-आधारित प per लाइब्रेरी के साथ सीमित एकीकरण
  • निर्देशों के साथ उपयोग करने के लिए सबसे अच्छा है

मूल README

संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।

Result Type for Dart

CI Status Result Type is released under the MIT license. PRs welcome!

Content

Features

Result is a type that represents either Success or Failure.

Inspired by functional programming, Rust and Swift.

Requirements

  • Dart: 3.3.0+

Install

dependencies:
  result_type: ^1.0.0

Example

The detailed example can be found at result_type/example/example.dart.

import 'dart:math';

import 'package:http/http.dart' as http;
import 'package:result_type/src/result.dart';

import 'imaginary_service.dart';
import 'photo.dart';
import 'photos_service.dart';

final client = http.Client();
final random = Random();

void main() async {
  final photosResult = await PhotosService.getPhotos(client);

  final photosOr = photosResult.unwrapOr([Photo.initial()]);

  print(photosOr);

  /// Use `switch` to handle both success and failure.
  final _ = switch (photosResult) {
    Success(value: final data) => data,
    Failure(value: final error) => 'Error: $error'
  };

  /// Do something with successful operation results or handle an error.
  if (photosResult.isSuccess) {
    print('Photos Items: ${photosResult.success}');
  } else {
    print('Error: ${photosResult.failure}');
  }

  // Chain multiple asynchronous operations.
  final result1 = await ImaginaryService.fetchData1();
  final result2 = await ImaginaryService.fetchData2(result1.success);
  final result3 = await ImaginaryService.fetchData2(result2.success);

  // Print the result of the last operation: `Success: Default Data`
  print(result3.unwrapOr('Default Data'));

  // This will throw an exception as `_handleResult`
  // has a case with 'Wrong Data'.
  //print(result3.unwrap());

  String length(String string) => string.length.toString();

  final unwrapOrElse = result3.unwrapOrElse('Default (((((Data)))))', length);
  // Print the result of the last operation: `22`
  print(unwrapOrElse);

  /// Apply transformation to successful operation results or handle an error.
  if (photosResult.isSuccess) {
    final items = photosResult
        .map(
          (i) => i.where(
            (j) => j.title.length > 60,
          ),
        )
        .success;
    print('Number of Long Titles: ${items.length}');
  } else {
    print('Error: ${photosResult.failure}');
  }

  /// In this example, note the difference in the result of using `map` and
  /// `flatMap` with a transformation that returns an result type.
  Result<int, Exception> getNextInteger() => Success(random.nextInt(4));
  Result<int, Exception> getNextAfterInteger(int n) =>
      Success(random.nextInt(n + 1));

  final nextIntegerNestedResults = getNextInteger().map(getNextAfterInteger);
  print(nextIntegerNestedResults.runtimeType);
  // Prints: Success<Result<int, Error>, dynamic>

  final nextIntegerUnboxedResults =
      getNextInteger().flatMap(getNextAfterInteger);
  print(nextIntegerUnboxedResults.runtimeType);
  // Prints: Success<int, Error>

  /// Use completion handler / callback style API if you want to.
  await PhotosService.getPhotos(client)
    ..result((photos) {
      // print('Photos: $photos');
    }, (error) {
      print('Error: $error');
    });
}

To see examples of the following package in action:

cd example && dart run

Support

Post issues and feature requests on the GitHub issue tracker.

License

The source code is distributed under the MIT license. See the LICENSE file for more info.

श्रेणियाँ

निर्भरताएँ

निर्भरता सूची 1 आइटम

सोर्स रिपॉज़िटरी

इस पैकेज का उपयोग करने वाले प्रोजेक्ट

अभी कोई डेटा उपलब्ध नहीं है।