Flutter Redux重击动作参数

cos*_*nus 2 redux flutter

我正在尝试使用redux_thunk,但是我从演示中真正不了解的是如何向该函数发送参数。我有一个文件actions.dart,其中包含所有动作。我想从我的组件中分配一些参数给该动作,以便向API发出请求。例如,我想使用用户名和密码登录,而不将其保存为状态

   actions.dart    
final ThunkAction<AppState> login = (Store<AppState> store) async {
          await new Future.delayed(
            new Duration(seconds: 3),
                () => "Waiting complete",
          );
          store.dispatch(OtherAction(....));
        };

    component.dart
class LoginBtn extends StatelessWidget {
          @override
          Widget build(BuildContext context) {
            return StoreConnector<AppState, Function>(
                converter: (store) => (){
                  login(store);
                },
                builder: (context, callback) {
                  return RaisedButton(
                      highlightElevation: 20.0,
                      child: Text('Login', style: TextStyle(color: Colors.white)),
                      color: Color(0xFF0f7186),
                      onPressed: () {
                        callback();
                      });
                });
          }
        }
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我进行一些快速修复或演示。谢谢!

像这样吗

class MyAction {
  String gender;

  MyAction(
      {this.gender});

  ThunkAction<AppState> getDate() {
    return (Store<AppState> store) async {...};
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

我认为最简单的方法是使用创建动作的函数:

ThunkAction<String> waitAndDispatch(int secondsToWait) {
   return (Store<String> store) async {
     final searchResults = await new Future.delayed(
       new Duration(seconds: secondsToWait),
       () => "Search Results",
     );

     store.dispatch(searchResults);
   };
 }
Run Code Online (Sandbox Code Playgroud)

然后,像这样发送:

store.dispatch(waitAndDispatch(3));
Run Code Online (Sandbox Code Playgroud)