搜索插件、代码仓与 APP 分类

插件详情v0.0.4

mobx_provider

用于轻松将 UI 与您的存储(视图模型)绑定的包,该包受填充栈架构模式启发

近 30 天下载
12
喜欢数
14
Pub 分数
40

安装插件

flutter pub add mobx_provider

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

MobX Provider 是一个轻量级包,可简化 Flutter 应用程序中 UI 与状态管理存储(视图模型)之间的连接。受 Clean Architecture 模式启发,它能够无缝集成 MobX 的响应式状态管理与 Flutter 的组件树。该包简化了依赖注入和状态访问,减少了样板代码并提高了代码可维护性。它支持高效的响应式更新,并在底层状态发生变化时自动更新 UI。

适合场景

  • 使用 MobX 存储的响应式 UI
  • 大型 Flutter 应用中的状态管理
  • 清洁架构实现
  • 视图模型的依赖注入
  • 简化跨小部件的商店访问

主要特点

  • 与 MobX 的轻松集成
  • 状态改变时自动重新构建 UI
  • 最小化样板代码
  • 支持嵌套的存储层次结构
  • 关注点的清晰分离

适合对象

  • 使用 MobX 进行状态管理的开发者
  • 遵循清洁架构的项目
  • 寻求简单、可扩展状态解决方案的团队
  • 具有复杂状态需求的 Flutter 应用
  • 需要响应式UI更新的应用程序

使用建议

  • 需要设置 MobX
  • 仅限基于 MobX 的状态管理
  • 无内置错误处理
  • 不适用于非 MobX 工作流
  • 高级用法可能需要额外的设置

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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 .

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目

暂无相关数据。