Package Analyzer

StateHolder

alt

StateHolder

StateHolder is A wrapper around InheritedWidget to make them easier to use and more reusable. By using StateHolder instead of manually writing InheritedWidget, you get:

  • simplified allocation of resources
  • lazy-loading
  • a vastly reduced boilerplate over making a new class every time
  • friendly to devtools
  • increased scalability for classes with a listening mechanism that grows >exponentially in complexity (such as ChangeNotifier, which is O(N) for dispatching notifications).

Add Dependencie

update in pubspec.ymal

dependencies:
  state_holder:
    git:
      url: https://github.com/mishrabroshan/state_holder.git

Compoenents

State Components Access Components
SimpleStateHolder SateHolder
ChangeNotifierStateHolder StateConsumer
MultiStateHolder StateHolderContext

State Components

SimpleStateHolder Component

Description : Use This Component To initialize SimpleStateHolder Which Will Just Hold The State Without Providing Any Change Updates

How To Use

  • Just Wrap This Component To the Widget And It is Ready To Roll
import 'package:state_holder/state_holder.dart';

SimpleStateHolder(
  builder : (context) => "[State]",
  lazy : true/false // Lazy-Loading,
  child : SomeWidget(),
),

ChangeNotifierStateHolder Component

Description : Use This Component To initialize ChangeNotifierStateHolder Which Will Hold The State And Providing Any Change Updates

[Note] : ChangeNotifierStateHolder can be only Used With Listnable Components

How To Use

  • Just Wrap This Component To the Widget And It is Ready To Roll
import 'package:state_holder/state_holder.dart';

ChangeNotifierStateHolder(
  builder : (context) => "[State]",
  lazy : true/false // Lazy-Loading,
  child : SomeWidget(),
),

MultiStateHolder Component

Description : A StateHolder that merges multiple StateHolders into a single linear widget tree. It is used to improve readability and reduce boilerplate code of having to nest multiple layers of StateHolders.

As such, we're going from:

import 'package:state_holder/state_holder.dart';

StateHolder<Something>(
  builder : (context) => Something(),
  child : StateHolder<SomethingElse>(
    builder : (context) => SomethingElse(),
    child StateHolder<AnotherThing>(
      builder : (context) => AnotherThing(),
      child : App(),
    ),
  ),
),

To:

import 'package:state_holder/state_holder.dart';

MultiStateHolder(
  stateHolders : [
    StateHolder<Something>(
      builder : (context) => Something()
    ),
    StateHolder<SomethingElse>(
      builder : (context) => SomethingElse(),
    ),
    StateHolder<AnotherThing>(
      builder : (context) => AnotherThing(),
    ),
  ],
  child : App(),
),

Access Components

StateHolder Component

Description : Provides Simple Interface To Access State Components From Any Where in Widget

StateHolder.of<T>(BuildContext context, [bool listen = false]) Use This Method To Obtain The Nearest StateHolder Where [T] is The Type of StateHolder Required And Context of Widget Requesting The State

Making Listen True Will Add This State To Dependant of The Context. Then Whenever The Value Inside State is Changed The Dependant is Rebuild. By Default The Parameter is False;

[Note] : Only ChangeNotifierStateHolder Has The Ablity To Rebuild Its Dependants.

Example :

import 'package:state_holder/state_holder.dart';

// Somewhere in Widget Tree

MultiStateHolder(
  stateHolders : [
    StateHolder(
      builder : (context) => "HelloWorld",
    ),
    ChangeNotifierStateHolder(
      builder : (context) => ValueNotifier(10),
    ),
  ],
  child : SomeWidget(),
),
class SomeWidget extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        var nonListnableDaa = StateHolder.of<String>(context);
        var listnableData = StateHolder.of<ValueNotifier<int>>(context, true);
        return AnotherWidget();
    }
}

StateConsumer Component

Description

Obtains StateHolder<T> from its ancestors and passes its value to builder. The StateCosumer widget doesn't do any fancy work. It just calls StateHolder.of in a new widget, and delegates its build implementation to builder. builder must not be null and may be called multiple times (such as when the StateHolder value change).

Example :

import 'package:state_holder/state_holder.dart';

// Somewhere in Widget Tree

StateHolder(
    builder : (context) => "HelloWorld",
    child : SomeWidget(),
)
Widget foo() {
  return StateConsumer<String>(
    builder : (context, state, child) {
      return SomeWidget();
    },
  );
}

StateHolderContext Component

Description : StateHolderContext is an Extension on BuildContext Which Provides Two Methods To Read And Watch State of StateHolder

context.readStateHolder<T>();

Use This Method To Read The State of StateHolde Without Listinig To Updates.

It Is Similer to :

StateHolder.of<T>(context, false);

context.watchStateHolder<T>();

Use This Method To Watch The State of StateHolde and Listinig To Updates

It Is Similer to :

StateHolder.of<T>(context, true);

Libraries

state_holder