Redux 颤振重击作用参数
我正在尝试使用redux_thunk,但从演示中我真正不明白的是如何向该函数发送参数。我有一个文件actions.dart你在哪里 拥有所有的行动。我想从我的组件中向该操作分派一些参数,以便向API发出请求。例如,我想使用用户名、密码登录,而不将其保存在状态Redux 颤振重击作用参数,redux,flutter,Redux,Flutter,我正在尝试使用redux_thunk,但从演示中我真正不明白的是如何向该函数发送参数。我有一个文件actions.dart你在哪里 拥有所有的行动。我想从我的组件中向该操作分派一些参数,以便向API发出请求。例如,我想使用用户名、密码登录,而不将其保存在状态 actions.dart final ThunkAction<AppState> login = (Store<AppState> store) async { await new
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();
});
});
}
}
actions.dart
最终ThunkAction登录=(存储)异步{
等待新的未来(
新的持续时间(秒:3),
()=>“等待完成”,
);
储存、派送(其他行动(……);
};
组件.省道
类LoginBtn扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回存储连接器(
转换器:(存储)=>(){
登录(商店);
},
生成器:(上下文,回调){
返回上升按钮(
Highlight标高:20.0,
子项:文本('Login',样式:TextStyle(颜色:Colors.white)),
颜色:颜色(0xFF0f7186),
已按下:(){
回调();
});
});
}
}
有人能帮我做一些快速修复或演示吗。谢谢
像这样的
class MyAction {
String gender;
MyAction(
{this.gender});
ThunkAction<AppState> getDate() {
return (Store<AppState> store) async {...};
}
}
类MyAction{
字符串性别;
我的行动(
{这是性别});
ThunkAction getDate(){
返回(存储)异步{…};
}
}
有两种方法可以做到这一点。其中两个将要在一个类中分派的操作包装起来,就像上面所做的一样
class MyAction {
String gender;
String name;
MyAction(this.gender, this.name);
void getDate<AppState>(Store<AppState> store) {
print("store is $store, gender is $gender, name is $name");
}
}
二,。我们没有修改或创建另一个中间件,而是将getDate()作为
ThunkAction并在分派之前调用getDate()。例如
class MyAction {
String gender;
String name;
MyAction(this.gender, this.name);
ThunkAction<AppState> getDate = (Store<AppState> store) {
print("store is $store, gender is $gender, name is $name");
}
}
第二种方法,即您在上面的示例中使用的方法,是我将如何进行操作,因为我不必干预中间件 我认为最简单的方法是使用函数创建动作:
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);
};
}
对于此功能,您应该基于官方文档扩展
CallableThunkAction
:
最终用户可以实现的接口,用于创建基于类的[ThunkAction]
样本
class SigninAction extends CallableThunkAction<AuthState> {
SigninAction(this.mobile);
final String mobile;
@override
call(Store<AuthState> store) async {
// call to server endpoint
store.dispatch(results);
}
}
当使用方法2)时,我得到了“只有静态成员才能在初始化器中访问”的答案,也许这个答案会帮助其他人
store.dispatch(new MyAction(...).getDate)
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);
};
}
store.dispatch(waitAndDispatch(3));
class SigninAction extends CallableThunkAction<AuthState> {
SigninAction(this.mobile);
final String mobile;
@override
call(Store<AuthState> store) async {
// call to server endpoint
store.dispatch(results);
}
}
store.dispatch(SigninAction('mobile number'));