micro_core_result 0.2.0 copy "micro_core_result: ^0.2.0" to clipboard
micro_core_result: ^0.2.0 copied to clipboard

This package is a simple approach to return multiple values, inspired on dartz package.

example/micro_core_result_example.dart

import 'package:micro_core_result/micro_core_result.dart';

const names = ['Johnson', 'Harvey', 'Marshall', 'Denver', 'Noelene', 'Coreen'];

class Repository {
  Future<Result<Exception, Empty>> createName(String name) async {
    try {
      if (name.isEmpty) throw Exception('An empty name is invalid!');
      await Future.delayed(const Duration(seconds: 2));
      return Right(empty);
    } on Exception catch (exception) {
      return Left(exception);
    }
  }

  Future<Result<Exception, List<String>>> listNames() async {
    try {
      await Future.delayed(const Duration(seconds: 2));
      return Right(names);
    } on Exception catch (exception) {
      return Left(exception);
    }
  }
}

class Controller {
  final Repository _repository;

  final names = <String>[];

  Controller(this._repository);

  void createName(String name) async {
    final result = await _repository.createName(name);

    result(
      (exception) => print('Exception => ${exception.toString()}'),
      (_) => print('Name was created successfully!'),
    );
  }

  void listNames() async {
    final result = await _repository.listNames();

    result(
      (exception) => print('Exception => ${exception.toString()}'),
      (namesList) {
        print('Names before: $names');
        names.addAll(namesList);
        print('Names after: $names');
      },
    );
  }
}

void main() {
  final repository = Repository();
  final controller = Controller(repository);

  controller.createName('');

  controller.createName('Dave');

  controller.listNames();
}
4
likes
0
points
54
downloads

Publisher

verified publisherjotapetecnologia.com.br

Weekly Downloads

This package is a simple approach to return multiple values, inspired on dartz package.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on micro_core_result