logo头像

我有一个梦想

fish-redux和flutter-redux学习

本文于 1648 天之前发表,文中内容可能已经过时。

flutter-redux流程图

redux流程图

原理

flutter_redux(0.5.2)作为工具类桥接Redux和Flutter,它提供了StoreProvider,StoreBuilder,StoreConnector这些组件,使我们在flutter中使用redux变的很简便。

image.png | left | 747x399

fish-redux的数据流向

数据流向

总处理页面

  • page中是最终形成的page页面,可以理解为index文件
  • page中绑定state,effect,reducer,view,middleware

基本用法

  • View布局中使用buildView搭建布局,关联store和触发dispatch事件,dispatch触发action事件
  • action事件中接受dispatch传过来的参数
  • 触发effect事件,effect中绑定不同的action事件和副作用的函数(eg:TextEditingController),再次触发dispatch事件
  • 触发reducer事件,根据action的类型去执行不同的逻辑,返回新的state,触发更新
  • view页面对改变的state数据页面进行重新渲染刷新
  • effect处理所有的数据处理,reducer只做数据的更新和生成新的state
  • effect文件不生成新的state,如果有网络请求处理,需要在生命中其中dispatch网络请求action成功后,再次action传递数据,在reducer中生成新的state,从而时page页面获取的state数据改变

问题

  1. fish-redux最终的页面处理是基于一个Component页面,该页面是一个StatefulWidget,所以,fish-redux的默认页面时StatefulWidget类型的,在需要StatelessWidget页面时,需要使用WidgetWrapper实现。

  2. 生命周期统一放置在effect中处理

  3. fish-redux推荐使用函数式编程方式,也提供面向对象式的编程方式。(java转过来的同学,对面向对象式的编程方式比较亲切,但是推荐使用函数式编程,不妨试一试)

  4. fish-redux使用identical比较新旧数据去决定是否需要更新,如果对刷新有特殊要求,可以自己定义一个ShouldUpdate

    1
    2
    3
    bool shouldUpdate(DetailState old, DetailState now) {
    return old.message != now.message;
    }
  5. 对Effect产生的业务进行统一异常处理,onError

    1
    2
    3
    4
    5
    6
    7
    8
    class MessageComponent extends Component<String> {
    MessageComponent(): super(
    view: buildMessageView,
    effect: buildEffect(),
    reducer: buildMessageReducer(),
    onError: onMessageError,
    );
    }
  6. page页面是继承于Component的,page的特色是可以配置Middleware,但是需要配置一个初始化页面数据的初始化函数initState,page页面可以接受一个参数,在创建页面时带参数进来

  7. component独立使用时需要进行buildComponent(x,x)两个参数

    1
    2
    3
    4
    5
    6
    7
    8
    class TestComponent {
    Widget test<T>(Component<T> component, T initState) {
    final PageStore<T> store = createPageStore<T>(
    initState, component.reducer,
    );
    return component.buildComponent(store, store.getState);
    }
    }

    需要component的state和reducer

    所以一般情况下都用page,参数问题

    adapter的时候