flutter - 使用 BLoC 的抽屉导航加载内容
问题描述
我正在尝试使用 BLoC 从我的抽屉中加载内容或页面。我正在使用flutter_bloc: ^4.0.0
菜单块
class MenuBloc extends Bloc<MenuEvent, MenuState> {
@override
MenuState get initialState => Dashboard();
@override
Stream<MenuState> mapEventToState(
MenuEvent event,
) async* {
if (event is DashboardClicked) {
yield Dashboard();
}
if (event is ProfileClicked) {
yield Profile();
}
}
}
菜单事件
abstract class MenuEvent{
@override
List<Object> get props => [];
}
class LoadDefaultMenu extends MenuEvent {}
class DashboardClicked extends MenuEvent {}
class ProfileClicked extends MenuEvent {}
菜单状态
abstract class MenuState extends Equatable {
const MenuState();
@override
List<Object> get props => [];
}
class Dashboard extends MenuState {}
class Profile extends MenuState {}
我有这个store
活动。
class NavModel {
String title;
IconData icon;
MenuEvent menuEvent;
NavModel({this.title, this.icon, this.menuEvent});
}
List<NavModel> navigationItems = [
NavModel(
title: 'Dashboard',
icon: Icons.insert_chart,
menuEvent: BlocProvider.of<MenuBloc>(context).add(
DashboardClicked(),
),
),
NavModel(
title: 'Profile',
icon: Icons.person,
menuEvent: BlocProvider.of<MenuBloc>(context).add(
ProfileClicked(),
),
),
];
但问题出在这部分List<NavModel> navigationItems
。我收到这个错误
错误:此表达式的类型为“void”,无法使用。menuEvent: BlocProvider.of(context).add( DashboardClicked(), ),
当我改变这个
menuEvent: MenuEvent.DashboardClicked
我得到这个错误
错误:找不到 Getter:“DashboardClicked”。menuEvent: MenuEvent.DashboardClicked
我该如何解决?我错过了什么?
解决方案
DashboardClicked是子类,
更改代码:
menuEvent: MenuEvent.DashboardClicked
到 :
menuEvent: DashboardClicked()
在NavModel类中
推荐阅读
- botframework - Microsoft Bot Framework 单元测试不继续对话
- adobe-analytics - Adobe Analytics - 首次添加购物车和结帐之间的时间
- regex - 将“notin”与匹配组一起使用
- sql - 如何选择给定数据是否存在于多个表中,然后更新到带有结果的表(只有一个表带来结果)
- apache-spark - 从 Spark 结构化流连接远程 Cassandra 节点
- android - 如何将多个 Firestore 任务串在一起?
- php - 使用 fgetcsv 读取 CSV 文件时出现逗号问题
- mysql - 全文索引找不到现有的区分大小写的记录
- javascript - (JavaScript) 创建一种比我现有的更有效的算法
- reactjs - 使用通过操作传递的参数填充 Redux Reducer