of<T> static method

T of<T>(
  1. BuildContext context, [
  2. bool listen = false
])

Use This Method To Obtain The Nearest StateHolder

T is The Type of StateHolder Required

Making Listen True Will Add This State To Dependant of The Context.

Then Whenever The Value Inside State is Changed The Dependant is Rebuild


Usage

import 'package:state_holder/state_holder.dart';
.....
.....

class SomeWidget extends ..... {
  void foo(BuildContext context) {
    StateHolder.of<T>(context, true/false);
  }
}

If The StateHolder is Not Found in Tree Then This Method Throws[StateHolderException]


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

Implementation

static T of<T>(BuildContext context, [bool listen = false]) {
  var element = _getElement<T>(context);
  if (element == null) {
    throw StateHolderException<T>();
  }

  if (listen && element.widget.listnable) {
    context.dependOnInheritedWidgetOfExactType<_InheritedStateHolder<T>>();
  }
  return element.getValue();
}