flutter - 如何使用 async_redux 实现路由导航
问题描述
我是 Flutter 和 Dart 的新手,在https://pub.dev/packages/async_redux中找到了一个新的“async_redux”包,以比传统的“redux”包更轻松地开发我的项目。在自述文件中有一个关于实现路线导航的简短描述,但我总是收到:
“类型'NavigateAction'不是'action'的'ReduxAction'类型的子类型”
当我在“onChangePage”中使用-dispatch(NavigateAction.pushNamed(“MyRoute”))时。
这里的结构代码:
Store<AppState> store;
final navigatorKey = GlobalKey<NavigatorState>();
void main() async{
NavigateAction.setNavigatorKey(navigatorKey);
var state = AppState.initialState();
store = Store<AppState>(initialState: state);
runApp(MyApp());
}
final routes={
'/': (BuildContext context) => First(),
"/myRoute": (BuildContext context) => Two(),
};
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StoreProvider<AppState>(
store: store,
child: MaterialApp(
routes: routes,
navigatorKey: navigatorKey,
),
);
}
}
class AppState {
AppState(...);
AppState copy(...) =>
AppState(
...
);
static AppState initialState() => AppState(
...
);
@override
bool operator ==(Object other) => ...
@override
int get hashCode => ...;
}
class First extends StatelessWidget {
@override
Widget build(BuildContext context) => MyHomePageConnector();
}
class MyHomePageConnector extends StatelessWidget {
MyHomePageConnector({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return StoreConnector<AppState, ViewModel>(
model: ViewModel(),
builder: (BuildContext context, ViewModel vm) => MyHomePage(
onChangePage: vm.onChangePage
),
);
}
}
class ViewModel extends BaseModel<AppState> {
ViewModel()
VoidCallback onChangePage;
ViewModel.build({
@required this.onChangePage,
}) : super(equals: []);
@override
ViewModel fromStore() => ViewModel.build(
onChangePage: () => dispatch (NavigateAction.pushNamed ("/myRoute"))
);
}
class MyHomePage extends StatefulWidget {
final VoidCallback onChangePage;
MyHomePage({
Key key,
this.onChangePage
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return RaisedButton(
child: Icon(Icons.add_circle),
onPressed: widget.onChangePage
),
);
}
}
如何以及在何处实现“dispatch(NavigateAction.pushNamed ("/myRoute"))”?
解决方案
尝试这个:
dispatch(NavigateAction<AppState>.pushNamed("/myRoute"))"
更新:
最近async_redux: ^1.2.0
你不再需要<AppState>
了,可以像这样发送它:
dispatch(NavigateAction.pushNamed("/myRoute"))"
推荐阅读
- c# - DynamoDb .AsyncSearch
.GetRemainingAsync 随机挂起 - process - 如何在 Rust 中杀死 Windows 上正在运行的子进程?
- php - PHP 数组对对象和值的工作方式不同
- node.js - 使用带有默认用户模型的环回 3.x 发送电子邮件
- php - 确定在控制器中选择了哪个 RadioType 表单
- android - [Ionic]从 Firebase 存储中检索图像的最佳方式是什么?
- jquery - 使用两个类切换导航
- c - C 指针:来自不兼容指针类型的赋值
- asp.net-mvc - Icollection 不包含任何
- spring-boot - 来自 Tomcat 的 Spring Boot 配置文件位置