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

패키지 상세v2.0.0

simple_observable

Future, Stream 및/또는 콜백을 사용하여 값 변경 사항을 관찰합니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
19.9만
좋아요
6
Pub 점수
160

패키지 설치

flutter pub add simple_observable

패키지 개요

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

simple_observable는 Futures, Streams 또는 콜백을 통해 값의 변경을 쉽게 관찰할 수 있도록 해주는 가벼운 Dart 패키지입니다. 뼈대 코드 없이 데이터 업데이트에 반응할 수 있는 명확한 인터페이스를 제공하여 반응형 프로그래밍을 단순화합니다. Flutter 앱에서 상태 관리에 이상적이며, Android, iOS, Web, macOS, Windows, Linux 등 여러 플랫폼을 지원합니다. 이 패키지는 비동기 데이터 흐름을 추적할 때 복잡성을 줄여 개발자 생산성을 높입니다.

사용 사례

  • 실시간 데이터 업데이트
  • Flutter 앱에서의 상태 관리
  • 이벤트 기반의 UI 업데이트
  • 비동기 데이터 스트림 처리
  • 콜백 로직 간소화

주요 기능

  • Futures 및 Streams를 지원합니다
  • 콜백 기반의 관찰
  • 최소한의 보일러플레이트
  • 크로스 플랫폼 호환성
  • 기존 코드와의 간편한 통합

적합한 대상

  • 간단한 상태 업데이트를 원하는 개발자
  • 반응형 UI를 가진 플러터 앱이 필요합니다
  • 무거운 상태 관리 솔루션을 피하는 프로젝트
  • 코드 간결성을 우선시하는 팀
  • 경량 관측이 필요한 앱

고려 사항

  • 복잡한 상태 트리에 적합하지 않습니다
  • 기본 관찰 패턴에 제한됨
  • 내장된 지속성 또는 되돌리기 기능 없음
  • 소규모에서 중규모 애플리케이션에 최적
  • 구독을 수동으로 처리해야 함

원본 README

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

pub.dev

simple_observable

Observe value changes using a Future, Stream, and/or a callback.

Source

class Observable<T> {
  Observable({T initialValue, this.onChanged, this.checkEquality = true})
      : _value = initialValue;

  /// If true, setting the [value] will only notifiy if the new value is different
  /// than the current value.
  final bool checkEquality;
  final void Function(T value) onChanged;

  var _completer = Completer<T>();

  bool _canceled = false;
  bool get canceled => _canceled;

  T _value;

  /// The current value of this observable.
  T get value => _value;
  set value(T val) {
    if (!canceled && (!checkEquality || _value != val)) {
      _value = val;
      // Delaying notify() allows the Future and Stream to update correctly.
      Future.delayed(Duration(microseconds: 1), () => notify(val));
    }
  }

  /// Alias for [value] setter. Good for passing to a Future or Stream.
  void setValue(T val) => value = val;

  @protected
  @mustCallSuper
  void notify(T val) {
    if (onChanged != null) onChanged(val);
    // Completing with a microtask allows a new completer to be constructed
    // before listeners of [nextValue] are called, allowing them to listen to
    // nextValue again if desired.
    _completer.complete(Future.microtask(() => val));
    _completer = Completer<T>();
  }

  Future<T> get nextValue => _completer.future;
  Stream<T> get values async* {
    while (!canceled) {
      yield await nextValue;
    }
  }

  /// Permanently disables this observable. Further changes to [value] will be
  /// ignored, the outputs [onChanged], [nextValue], and [values] will not be
  /// called again.
  @mustCallSuper
  void cancel() => _canceled = true;
}

카테고리

의존성

의존성 목록 1 개

소스 저장소

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