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

패키지 상세v2.1.1

generic_bloc_provider

Flutter 앱용 일반적인 BloC 제공자입니다. 이 패키지는 BloC 제공자를 작성할 때 반복되는 코드를 피하는 데 도움이 됩니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
561
좋아요
22
Pub 점수
160

패키지 설치

flutter pub add generic_bloc_provider

패키지 개요

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

블록 인스턴스를 설정하고 관리하는 데 일반적으로 필요한 부품 코드를 줄이기 위해 일반적인 블록 제공자를 제공함으로써 상태 관리를 단순화하는 플러터 패키지입니다. 개발자가 타입 안전성과 최소한의 설정으로 위젯 트리 전체에서 블록 클래스를 쉽게 주입하고 액세스할 수 있게 해줍니다. 블록 패턴을 사용하는 앱에 적합하며, 종속성 주입을 간소화하고 코드 유지보수성을 향상시킵니다.

사용 사례

  • 대규모 앱에서 BloC 인스턴스화 간소화
  • 반복적인 공급자 보일러플레이트 줄이기
  • 여러 BloC 인스턴스를 효율적으로 관리하기
  • BloCs에 대한 타입 안전한 액세스 활성화
  • 관심사의 명확한 분리 지원

주요 기능

  • BloC 클래스의 제네릭 타입 지원
  • 최소한의 보일러플레이트 코드
  • 타입 안전한 공급자 접근
  • 기존 BloC 패턴과의 간편한 통합
  • 자동 수명 주기 관리

적합한 대상

  • BloC 패턴을 사용하는 개발자
  • 반복 코드를 줄이려는 프로젝트
  • 여러 상태 관리자를 가진 앱
  • 유지보수 가능한 상태 로직을 우선시하는 팀
  • 다중 플랫폼에서의 Flutter 프로젝트

고려 사항

  • BloC 패턴에 대한 이해가 필요합니다
  • 간단한 상태 요구 사항에 적합하지 않습니다
  • 작은 앱에 대해 복잡성을 증가시킬 수 있음
  • 다른 상태 관리 도구와 함께 사용하는 것이 가장 좋습니다
  • 수동 제공자에 비해 사용자 지정이 제한됨

원본 README

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

generic_bloc_provider

Build Status License: MIT Pub

A generic BloC Provider for your Flutter apps.

Getting Started

All your BloCs must extend the Bloc abstract class. This means that all of them should have a dispose method that will be executed whenever the life of the BloC comes to an end.

In this method, you should take care of closing all the sinks and resources.

Example of how to use it:

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      bloc: AppBloc(),
      child: MaterialApp(
        title: 'Yo Sleep',
        home: MainPage(),
        initialRoute: 'main',
        routes: {
          'main': (context) => MainPage(),
        },
      ),
    );
  }
}

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appBloc = BlocProvider.of<AppBloc>(context);
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Header(),
            RecordList(appBloc),
          ],
        ),
      ),
    );
  }
}

Attaching your context to the InheritedWidget

The BlocProvider class depends on InheritedWidget. Whenever you want to get your BloC, you can decide wether to attach the context of your widget to the InheritedWidget or not.

In order to control this behavior, the static method of has an optional boolean argument (which is true by default) which determines wether your context will be attached or not.

Basically, if you don't provide it or you just set it to true, dependOnInheritedWidgetOfExactType will be used. If you set it to false then getElementForInheritedWidgetOfExactType will be used instead.

Customizing the update policy

If you want to change the way updateShouldNotify behaves you have the option to provide a custom anonymous function to the BlocProvider constructor.

You will notice that there's an argument called updateShouldNotifyOverride which accepts a function receiving a BloC and the internal InheritedWidget:

bool Function(T bloc, _BlocProvider oldWidget);

This is the default implementation of the updateShouldNotify method:

@override
  bool updateShouldNotify(_BlocProvider oldWidget) =>
      updateShouldNotifyOverride != null
          ? updateShouldNotifyOverride!(bloc, oldWidget)
          : oldWidget.bloc != bloc;

카테고리

소스 저장소

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