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

GitHub 저장소kaarimtareek/mobx_provider

mobx_provider

UI를 스토어(뷰모델)와 쉽게 바인딩할 수 있는 패키지로, 채워진 스택 아키텍처 패턴에서 영감을 받았습니다

mobx_provider 프로젝트 이미지
스타
3
포크
4
최근 푸시(UTC)
2020. 7. 25.
프로젝트 상태
활성

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

mobx_provider

Additional package for Mobx, it allows you to bind your (viewmodels) mobx stores with UI more easily the package is inspired by filled stacks pattern [view model provider].

Getting Started

NOTE : if you are not familiar with Mobx, take a look on it to see how you can use this package.

what's new in version 0.0.3

  • The packages now depends on the latest versions of [mobx],[flutter_mobx],[provider] see the change log for thier versions
  • Fix MobxStatefulProvider exception on dispose , Thanks to wbemanuel
  • change [Provider.of(context)] to [Context.read()]

Breaking Changes

  • in older versions (before 0.0.2) MobxStatefulProvider now changed to MobxStatefulObserver and MobxStatelessProvider now changed to MobxStatelessObserver with the same functionalities
  • in the new version (0.0.2) the MobxStatefulProvider widget is integration between Mobx and Provider package it contains Provider.of<T>(context) where T extends MobxBase , this widget exposes only the value of the store wihtout observing it
  • MobxWidgetProvider uses also Provider to get the value of the store and observe it in specific widget (see the example to have a better understanding)

1 - create you own store and make it extends MobxBase class and write your own logic

class CounterStore = _CounterStore with _$CounterStore;
abstract class _CounterStore extends MobxBase with Store {

  
  _CounterStore():_counter=0;
  @observable
  int _counter;

  int get counter => _counter;

  @action
  void increment() => _counter++;
  @override
  void dispose() {}
}

NOTE:

  • the class MobxBase contains a dispose function which should be implemented in your store .
  • additionally, it has some helper functions and states which indicates the state of your store whether it's loading ,error ,initial,success.

2- bind your logic with the UI like this

class CounterWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MobxStatefulObserver<CounterStore>(
      store: CounterStore(),
      initFunction:(store){
        //do api calls here
        //store.fetchData();
      }
      builder: (context, store) {
        return Scaffold(
          floatingActionButton: FloatingActionButton(
            onPressed: store.increment,
          ),
          appBar: AppBar(
            title: Text('mobx provider example'),
          ),
          body: Center(
            child: Column(
              children: <Widget>[
                Text('counter value is ${store.counter}'),
              ],
            ),
          ),
        );
      },
    );
  }
}
  • MobxStatefulObserver is a stateful widget that takes a generic type which extends MobxBase .
  • it contains initFunction that's called in initState if you wanted to run some code when the widget is rendered for the first time
  • it'll call the dispose function when the widget it removed from the widget tree.

If you want to have more optimization when rendring the widget tree

  • you can use MobxStatefulProvider which exposes only the store from the provider package (donsn't observe any state)
  • use MobxWidgetProvider to observe the state of the store and only this widget will be rendered when the store changes
class CounterWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MobxStatefulProviderr<CounterStore>(
      initFunction: (store){
        //do api calls here
        //store.fetchData();
      }
      builder: (context, store) {
        return Scaffold(
          floatingActionButton: FloatingActionButton(
            onPressed: store.increment,
          ),
          appBar: AppBar(
            title: Text('mobx provider example'),
          ),
          body: Center(
            child: Column(
              children: <Widget>[
               MobxWidgetProvider<CounterStore>(
                  builder:(context,store){
                    //this widget will only be rendered when the value changes
                    return Text('counter value is ${store.counter}');
                  }
               ) 
              ],
            ),
          ),
        );
      },
    );
  }
}

if you have any suggestions or improvments please let me know .

Karim Tarek GitHub avatar
GitHub 작성자

Karim Tarek ↗

Passionate software engineer Backend development C# / DotNet Core

Cairo, Egypt
언어DartKotlinSwiftObjective-C

이 저장소가 배포한 패키지

사용 중인 의존성

의존성 목록 7 개

프로젝트에 맞는 저장소를 선택하려면?

사용 목적에 따라 검색하고 개발 활동을 비교한 뒤 원본 저장소에서 요구 사항을 확인하세요.

  1. 이름, 작성자 또는 설명으로 검색하고 기술 주제로 범위를 좁힙니다.
  2. 스타, 포크, 최근 푸시를 함께 비교하세요. 스타는 관심의 지표이며 품질을 보장하지 않습니다.
  3. 사용하기 전에 GitHub에서 README, 라이선스, Issues를 확인하세요.

출처 및 관리

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗