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

패키지 상세v2.0.0+5

mu_state

Flutter 내장 기본 유형을 사용한 최소한의 Cubit 스타일 상태 관리. 외부 종속성 없음.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
29
좋아요
2
Pub 점수
160

패키지 설치

flutter pub add mu_state

패키지 개요

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

mu_state는 Cubit에서 영감을 받은 가볍고 종속성 없는 Flutter용 상태 관리 솔루션입니다. Stream과 ValueNotifier와 같은 Flutter 내장 기본 요소를 활용하여 외부 종속성이 없이 최소한의 효율적인 앱 상태 관리를 제공합니다. 단순성과 성능을 고려해 설계되었으며, 위젯에서 상태 변경을 발생시키고 반응하는 명확한 API를 제공합니다. 전체 기능을 갖춘 상태 관리 라이브러리가 과도할 수 있는 소규모부터 중간 규모의 앱에 적합합니다.

사용 사례

  • 작은 앱에서의 간단한 상태 관리
  • 복잡한 상태 솔루션 교체
  • Bloc/Cubit의 경량 대안
  • 최소한의 오버헤드로 실시간 UI 업데이트
  • 모듈형 또는 마이크로서비스 유사 아키텍처에서의 상태 처리

주요 기능

  • 외부 종속성 없음
  • Flutter의 네이티브 기본 요소 위에 구축됨
  • 최소한의 보일러플레이트
  • Cubit을 영감으로 한 API
  • 기존 Flutter 코드와의 간편한 통합
  • 스트림 기반 상태 방출

적합한 대상

  • 경량 상태 관리를 찾는 개발자
  • 제3자 라이브러리를 피하는 프로젝트
  • 빠른 상태 업데이트가 필요한 앱
  • 상태 관리 개념 학습
  • 간단함과 유지보수성을 우선시하는 팀

고려 사항

  • 매우 대규모 애플리케이션에는 적합하지 않습니다
  • 제한된 내장 오류 처리
  • 내장된 지속성 또는 직렬화 없음
  • 수동 상태 전이 로직 필요
  • Bloc과 같은 완전한 프레임워크보다 더 주관적이지 않음

원본 README

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

mu_state

Minimal Cubit-inspired state management using Flutter's built-in primitives. No external dependencies.

See my article From ValueNotifier to Cubit-inspired state management for more info

Overview

mu_state provides a lightweight alternative to Bloc/Cubit using Flutter's built-in ValueNotifier, ValueListenableBuilder and InheritedWidget. It follows the same patterns you know from Bloc but with zero dependencies and minimal boilerplate.

Key concepts:

  • MuLogic<S> - Your business logic (like Cubit)
  • MuBuilder<S> - Rebuilds UI on state changes (like BlocBuilder)
  • MuListener<S> - Performs side effects (like BlocListener)
  • MuConsumer<S> - Combines builder and listener (like BlocConsumer)
  • MuProvider<T> - Provides values down the widget tree (like Provider or BlocProvider)

Additional widgets are available for handling multiple states or providers: MuMultiBuilder, MuMultiListener, and MuMultiProvider. The MuComparable mixin is also available for state equality comparison. See the Components section for details.

Screenshots

Usage

Let's create a simple counter to see how mu_state works:

counter_logic.dart
import 'package:mu_state/mu_state.dart';

class CounterState with MuComparable {
  final int counter;
  final bool isLoading;
  final String? error;

  const CounterState({required this.counter, required this.isLoading, this.error});

  CounterState copyWith({int? counter, bool? isLoading, String? error}) {
    return CounterState(
      counter: counter ?? this.counter,
      isLoading: isLoading ?? this.isLoading,
      error: error ?? this.error,
    );
  }

  @override
  List<Object?> get props => [counter, isLoading, error];
}

class CounterLogic extends MuLogic<CounterState> {
  CounterLogic() : super(const CounterState(counter: 0, isLoading: false));

  void increment() {
    value = value.copyWith(counter: value.counter + 1);
  }

  Future<void> incrementAsync() async {
    value = value.copyWith(isLoading: true, error: null);
    await Future.delayed(const Duration(seconds: 1));
    
    // Simulate random error
    if (DateTime.now().millisecondsSinceEpoch % 3 == 0) {
      value = value.copyWith(isLoading: false, error: 'Failed to increment');
    } else {
      value = value.copyWith(counter: value.counter + 1, isLoading: false);
    }
  }
}
main.dart
import 'package:flutter/material.dart';
import 'package:mu_state/mu_state.dart';
import 'counter_logic.dart';

void main() => runApp(CounterApp());

class CounterApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MuProvider<CounterLogic>(
        value: CounterLogic(),
        child: CounterPage(),
      ),
    );
  }
}
counter_page.dart
class CounterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final logic = context.logic<CounterLogic>();
    
    return Scaffold(
      appBar: AppBar(title: Text('Counter')),
      body: MuListener<CounterState>(
        logic: logic,
        listener: (context, state) {
          if (state.error != null) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('⚠️ ${state.error}')),
            );
          }
        },
        listenWhen: (prev, curr) => prev.error != curr.error && curr.error != null,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              MuBuilder<CounterState>(
                valueListenable: logic,
                builder: (context, state, child) {
                  return Column(
                    children: [
                      Text('Counter: ${state.counter}', style: Theme.of(context).textTheme.headlineMedium),
                      if (state.isLoading) CircularProgressIndicator(),
                    ],
                  );
                },
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () => logic.increment(),
                child: Text('Increment'),
              ),
              SizedBox(height: 8),
              ElevatedButton(
                onPressed: () => logic.incrementAsync(),
                child: Text('Async Increment'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

At this point we have successfully separated our presentational layer from our business logic layer. Notice that CounterPage knows nothing about what happens when a user taps the buttons. The widget simply notifies the CounterLogic that the user has pressed increment.

Alternative: You could also use MuConsumer to combine the listener and builder functionality in a single widget. See the MuConsumer section below for details.

Components

MuLogic

MuLogic<S> is a typedef for ValueNotifier<S> that serves as the foundation for your business logic. It manages state and notifies listeners when the state changes. MuLogic is essentially an alias that makes the code more semantic for state management purposes.

class CounterLogic extends MuLogic<CounterState> {
  CounterLogic() : super(const CounterState(counter: 0, isLoading: false));

  void increment() {
    value = value.copyWith(counter: value.counter + 1);
  }
}

Since MuLogic extends ValueNotifier, you get all the familiar methods like addListener, removeListener, and the value property for getting and setting state.

MuBuilder

MuBuilder<S> is a typedef for ValueListenableBuilder<S> that rebuilds the widget when the state changes. It's essentially an alias that makes the code more semantic for state management, but underneath it's just Flutter's built-in ValueListenableBuilder.

See MuListener if you want to "do" anything in response to state changes such as navigation, showing a dialog, etc...

MuBuilder<CounterState>(
  valueListenable: logic,
  builder: (context, state, child) {
    return Text('Counter: ${state.counter}');
  }
)
MuComparable

MuComparable is a mixin that provides equality comparison for state classes. It's a lightweight alternative to packages like Equatable and helps MuBuilder and other listeners determine when to rebuild.

class CounterState with MuComparable {
  final int counter;
  final String? error;

  const CounterState({required this.counter, this.error});

  @override
  List<Object?> get props => [counter, error];
}

The props list should include all properties that determine equality. When state changes, widgets will only rebuild if the new state is different from the previous state based on these properties.

Updating collections

Equality is shallow: entries in props are compared with Dart's default ==. For List, Map, and Set that means reference equality, not content equality. Always create a new collection when updating state — never mutate in place:

// ✅ Correct — new list reference, listeners fire.
emit(state.copyWith(items: [...state.items, newItem]));

// ❌ Wrong — same reference, listeners do NOT fire.
state.items.add(newItem);
emit(state.copyWith(items: state.items));

If you'd rather have deep collection equality out of the box, equatable is a drop-in alternative with the same props API.

MuListener

MuListener performs side effects in response to state changes - navigation, showing dialogs, etc. The listener is called once per state change and by default NOT on initial state (lazy: true). Set lazy: false to call the listener immediately with the current state.

MuListener<CounterState>(
  logic: logic,
  listener: (context, state) {
    if (state.error != null) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error: ${state.error}')),
      );
    }
  },
  listenWhen: (prev, curr) => prev.error != curr.error,
  child: Container(),
)
MuProvider

MuProvider is a Flutter widget which provides any value to its children via dependency injection. While originally designed for MuLogic instances, it's now fully generic and can provide any type - making it perfect for injecting repositories, services, or other dependencies throughout your widget tree.

You provide the instance to MuProvider via the value parameter. MuProvider will automatically handle disposing the value when the provider is disposed (if it implements ChangeNotifier, which MuLogic does).

MuProvider<CounterLogic>(
  value: CounterLogic(),
  child: CounterPage(),
);

Access from anywhere in the subtree:

// For MuLogic instances
final logic = context.logic<CounterLogic>();

// For any type
final repo = context.read<AuthRepository>();
MuMultiProvider

MuMultiProvider is a Flutter widget that merges multiple MuProvider widgets into one. MuMultiProvider improves the readability and eliminates the need to nest multiple MuProviders. By using MuMultiProvider we can go from:

MuProvider<LogicA>(
  value: LogicA(),
  child: MuProvider<LogicB>(
    value: LogicB(),
    child: MuProvider<LogicC>(
      value: LogicC(),
      child: ChildA(),
    )
  )
)

to:

MuMultiProvider([
  (child) => MuProvider<LogicA>(value: LogicA(), child: child),
  (child) => MuProvider<LogicB>(value: LogicB(), child: child),
  (child) => MuProvider<LogicC>(value: LogicC(), child: child),
], child: ChildA())
MuMultiBuilder

MuMultiBuilder is a Flutter widget which listens to multiple MuLogic instances and rebuilds when any of them change. This is useful when you need to build UI that depends on multiple state sources.

MuMultiBuilder(
  listenables: [logicA, logicB],
  builder: (context, values, child) {
    final stateA = values[0] as StateA;
    final stateB = values[1] as StateB;
    return Text('A: ${stateA.value}, B: ${stateB.value}');
  },
)
MuMultiListener

MuMultiListener is a Flutter widget that merges multiple MuListener widgets into one. MuMultiListener improves the readability and eliminates the need to nest multiple MuListeners. By using MuMultiListener we can go from:

MuListener<StateA>(
  logic: logicA,
  listener: (context, state) {
    // handle state A changes
  },
  child: MuListener<StateB>(
    logic: logicB,
    listener: (context, state) {
      // handle state B changes
    },
    child: ChildWidget(),
  ),
)

to:

MuMultiListener(
  listeners: [
    (child) => MuListener<StateA>(
      logic: logicA,
      listener: (context, state) {
        // handle state A changes
      },
      child: child,
    ),
    (child) => MuListener<StateB>(
      logic: logicB,
      listener: (context, state) {
        // handle state B changes
      },
      child: child,
    ),
  ],
  child: ChildWidget(),
)
MuConsumer

MuConsumer combines MuBuilder and MuListener functionality in a single widget.

MuConsumer<CounterState>(
  logic: logic,
  listener: (context, state) {
    if (state.error != null) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error: ${state.error}')),
      );
    }
  },
  builder: (context, state, child) {
    return Text('Counter: ${state.counter}');
  },
)

An optional listenWhen and buildWhen can be implemented for more granular control over when listener and builder are called. The listenWhen and buildWhen functions take the previous state and the current state and return a bool which determines whether or not the listener or builder function will be invoked.

MuConsumer<CounterState>(
  logic: logic,
  listenWhen: (previous, current) {
    // return true/false to determine whether or not
    // to invoke listener with state
    return previous.error != current.error;
  },
  listener: (context, state) {
    // do stuff here based on state
  },
  buildWhen: (previous, current) {
    // return true/false to determine whether or not
    // to rebuild the widget with state
    return previous.counter != current.counter;
  },
  builder: (context, state, child) {
    // return widget here based on state
    return Text('Counter: ${state.counter}');
  }
)

See the example project for a complete implementation with more features.

카테고리

소스 저장소

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

아직 데이터가 없습니다.