패키지, 저장소 및 앱 카테고리 검색

패키지 상세v2.2.0

result_command

result_dart 패키지를 사용한 Dart 및 Flutter용 명령 패턴 구현.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
4.8천
좋아요
33
Pub 점수
160

패키지 설치

flutter pub add result_command

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

result_command는 result_dart 기반을 사용하여 명령 패턴을 구현하는 Dart 및 Flutter 패키지입니다. 요청을 객체로 캡슐화하는 구조적인 방법을 제공하여 관심사의 분리, 되돌리기 작업, 로깅을 개선합니다. 깔끔한 아키텍처를 위해 설계되어 명령과 실행을 분리함으로써 Flutter 애플리케이션의 테스트 가능성과 유지보수성을 높입니다. 기존 Dart 코드베이스와 원활하게 통합되며 Android, iOS, Web, macOS, Windows, Linux 등 모든 주요 플랫폼을 지원합니다.

사용 사례

  • 취소/다시 실행 기능 구현
  • 사용자 인터페이스를 비즈니스 로직에서 분리
  • 모듈러하고 테스트 가능한 명령 체인 구축
  • 결과가 있는 비동기 작업 처리
  • 거래 워크플로 지원

주요 기능

  • 명령 패턴 구현
  • result_dart와의 통합
  • 비동기 명령어 지원
  • 관심사의 명확한 분리
  • 확장 가능하고 재사용 가능한 명령 구조

적합한 대상

  • 클린 아키텍처를 사용하는 개발자
  • 강력한 명령 처리가 필요한 팀
  • 트랜잭션 동작이 필요한 플러터 앱
  • 테스트 가능성과 유지보수성을 강조하는 프로젝트

고려 사항

  • 명령 패턴에 대한 이해가 필요합니다
  • result_dart 패키지에 의존합니다
  • 간단한 사용 사례에 대한 오버헤드를 추가할 수 있습니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

Result Command

Result Command is a lightweight package that implements the Command Pattern in Flutter, enabling encapsulation of actions, state tracking, and result management.


Installation

Add the package to your pubspec.yaml:

dependencies:
  result_command: x.x.x
  result_dart: 2.x.x

How to Use

1. Creating a Command

Commands encapsulate actions and manage their lifecycle. Depending on the number of parameters your action requires, you can use:

  • Command0: For actions with no parameters.
  • Command1: For actions with one parameter.
  • Command2: For actions with two parameters.

Example:

final fetchGreetingCommand = Command0<String>(
  () async {
    await Future.delayed(Duration(seconds: 2));
    return Success('Hello, World!');
  },
);

final calculateSquareCommand = Command1<int, int>(
  (number) async {
    if (number < 0) {
      return Failure(Exception('Negative numbers are not allowed.'));
    }
    return Success(number * number);
  },
);

2. Listening to a Command

Commands are Listenable, meaning you can react to their state changes:

Using addListener
fetchGreetingCommand.addListener(() {
  final status = fetchGreetingCommand.value;
  if (status is SuccessCommand<String>) {
    print('Success: ${status.value}');
  } else if (status is FailureCommand<String>) {
    print('Failure: ${status.error}');
  }
});
Using ValueListenableBuilder
Widget build(BuildContext context) {
  return ListenableBuilder(
    listenable: fetchGreetingCommand,
    builder: (context, _) {
      return switch (state) {
        RunningCommand<String>() => CircularProgressIndicator(),
        SuccessCommand<String>(:final value) => Text('Success: $value'),
        FailureCommand<String>(:final error) => Text('Failure: $error'),
        _ => ElevatedButton(
          onPressed: () => fetchGreetingCommand.execute(),
          child: Text('Fetch Greeting'),
        ),
      };
    },
  );
}
Using when for Simplified State Handling

The when method simplifies state management by mapping each state to a specific action or value:

fetchGreetingCommand.addListener(() {
  final status = fetchGreetingCommand.value;
  final message = status.when(
    data: (value) => 'Success: $value',
    failure: (exception) => 'Error: ${exception?.message}',
    running: () => 'Fetching...',
    orElse: () => 'Idle',
  );

  print(message);
});

This approach ensures type safety and provides a clean way to handle all possible states of a command.


3. Executing a Command

The execute() method triggers the action encapsulated by the command. During execution, the command transitions through the following states:

  1. Idle: The initial state, indicating the command is ready to execute.
  2. Running: The state while the action is being executed.
  3. Success: The state when the action completes successfully.
  4. Failure: The state when the action fails.
  5. Cancelled: The state when the action is cancelled.

Each command can only be executed one at a time. If another call to execute() is made while the command is already running, it will be ignored until the current execution finishes or is cancelled.

Example:

fetchGreetingCommand.execute();

4. Cancelling a Command

Commands can be cancelled if they are in the Running state. When cancelled, the command transitions to the Cancelled state and invokes the optional onCancel callback.

Example:

final uploadCommand = Command0<void>(
  () async {
    await Future.delayed(Duration(seconds: 5));
  },
  onCancel: () {
    print('Upload cancelled');
  },
);

uploadCommand.execute();

Future.delayed(Duration(seconds: 2), () {
  uploadCommand.cancel();
});

5. Facilitators

To simplify interaction with commands, several helper methods and getters are available:

State Check Getters

These getters allow you to easily check the current state of a command:

  if (command.isRunning) {
    print('Command is idle and ready to execute.');
  }
Cached Values

To avoid flickering or unnecessary updates in the UI, commands cache their last success and failure states:

  • getCachedSuccess(): Retrieves the cached value of the last successful execution, or null if no success is cached.
    final successValue = command.getCachedSuccess();
    if (successValue != null) {
      print('Last successful value: $successValue');
    }
    
  • getCachedFailure(): Retrieves the cached exception of the last failed execution, or null if no failure is cached.
    final failureException = command.getCachedFailure();
    if (failureException != null) {
      print('Last failure exception: $failureException');
    }
    

These facilitators improve code readability and make it easier to manage command states and results efficiently.


6. Filtering Command State

The filter method allows you to derive a new value from the command's state using a transformation function. This is useful for creating filtered or transformed views of the command's state.

Example:
final filteredValue = command.filter<String>(
  'Default Value',
  (state) {
    if (state is SuccessCommand<String>) {
      return 'Success: ${state.value}';
    } else if (state is FailureCommand<String>) {
      return 'Error: ${state.error}';
    }
    return null; // Ignore other states.
  },
);

filteredValue.addListener(() {
  print('Filtered Value: ${filteredValue.value}');
});

This method simplifies state management by allowing you to focus on specific aspects of the command's state.


7. CommandRef

The CommandRef class allows you to create commands that listen to changes in one or more ValueListenables or Streams and execute actions based on derived values.

Example with ValueListenable:
final listenable = ValueNotifier<int>(0);

final commandRef = CommandRef<int, int>(
  (ref) => ref(listenable),
  (value) async => Success(value * 2),
);

commandRef.addListener(() {
  final status = commandRef.value;
  if (status is SuccessCommand<int>) {
    print('Result: ${status.value}');
  }
});

listenable.value = 5; // Executes the command with the value 5.
Example with Stream:
final streamController = StreamController<int>();

final commandRef = CommandRef<int, int>(
  (ref) => ref.stream(streamController.stream, 0), // 0 is the initial value
  (value) async => Success(value * 3),
);

commandRef.addListener(() {
  final status = commandRef.value;
  if (status is SuccessCommand<int>) {
    print('Result: ${status.value}');
  }
});

streamController.add(10); // Executes the command with the value 10.
Features:
  • Automatically listens to changes in ValueListenables using ref(listenable).
  • Converts Streams to synchronous values using ref.stream(stream, initialValue).
  • Executes the action whenever the derived value changes.
  • Processes executions sequentially in a queue to avoid race conditions.
  • Cleans up listeners and stream subscriptions when disposed.

This class is ideal for scenarios where commands need to react dynamically to external state changes from multiple sources.

Documentation

For advanced examples and detailed documentation, visit:


Contribute

We welcome contributions! Feel free to report issues, suggest features, or submit pull requests.

카테고리

의존성

의존성 목록 1 개

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.