generic_usecase 1.3.0 copy "generic_usecase: ^1.3.0" to clipboard
generic_usecase: ^1.3.0 copied to clipboard

Small component that encapsulates an application's scenario logic.

example/lib/example.dart

// Copyright 2023 Hugo Pointcheval
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:async';

import 'package:example/failure.dart';
import 'package:sealed_result/sealed_result.dart';
import 'package:generic_usecase/generic_usecase.dart';

class AdditionUsecase extends Usecase<int, int> {
  const AdditionUsecase();

  @override
  Future<int> execute(int params) async {
    return params + params;
  }
}

class DivisionUsecase extends Usecase<(int, int), double> {
  const DivisionUsecase();

  @override
  FutureOr<PreconditionsResult> checkPrecondition((int, int)? params) {
    if (params == null) {
      return PreconditionsResult(isValid: false, message: 'Params is null');
    }

    if (params.$2 == 0) {
      return PreconditionsResult(isValid: false, message: 'Cannot divide by 0');
    }

    return PreconditionsResult(isValid: true);
  }

  @override
  Future<double> execute((int, int) params) async {
    return params.$1 / params.$2;
  }
}

class GeneratorUsecase extends NoParamsStreamUsecase<int> {
  const GeneratorUsecase();

  @override
  Stream<int> execute() async* {
    for (int i = 0; i < 10; i++) {
      yield i;
    }
  }
}

class DivisionResultUsecase extends ResultUsecase<(int, int), double, Failure> {
  const DivisionResultUsecase();

  @override
  FutureOr<PreconditionsResult> checkPrecondition((int, int)? params) {
    if (params == null) {
      return PreconditionsResult(isValid: false, message: 'Params is null');
    }

    if (params.$2 == 0) {
      return PreconditionsResult(isValid: false, message: 'Cannot divide by 0');
    }

    return PreconditionsResult(isValid: true);
  }

  @override
  Future<Result<double, Failure>> execute((int, int) params) async {
    return Result.success(params.$1 / params.$2);
  }

  @override
  Result<double, Failure> onException(UsecaseException e) =>
      Result.failure(Failure(e.message ?? ''));
}
3
likes
160
pub points
55%
popularity

Publisher

verified publisherhugop.cl

Small component that encapsulates an application's scenario logic.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

meta, sealed_result

More

Packages that depend on generic_usecase