0

我正在尝试使用 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

我该如何解决?我错过了什么?

4

1 回答 1

0

DashboardClicked是子类,

更改代码:

menuEvent: MenuEvent.DashboardClicked

到 :

menuEvent: DashboardClicked()

NavModel类中

于 2020-05-11T22:47:34.130 回答