简体   繁体   中英

Drawer navigiation load content with BLoC

I'm trying to load content or page from my drawer with BLoC. I'm using flutter_bloc: ^4.0.0

MenuBloc

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();
    }
  }
}

MenuEvent

abstract class MenuEvent{
  @override
  List<Object> get props => [];
}

class LoadDefaultMenu extends MenuEvent {}

class DashboardClicked extends MenuEvent {}

class ProfileClicked extends MenuEvent {}

MenuState

abstract class MenuState extends Equatable {
  const MenuState();

  @override
  List<Object> get props => [];
}

class Dashboard extends MenuState {}
class Profile extends MenuState {}

and i have this to store the event.

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(),
      ),
    ),
  ];

But the problem is on this part List<NavModel> navigationItems . I get this error

Error: This expression has type 'void' and can't be used. menuEvent: BlocProvider.of(context).add( DashboardClicked(), ),

when i change to this

menuEvent: MenuEvent.DashboardClicked

i get this error

Error: Getter not found: 'DashboardClicked'. menuEvent: MenuEvent.DashboardClicked

How can i fix it? did i miss something?

the DashboardClicked is sub class,

change code from:

menuEvent: MenuEvent.DashboardClicked

to:

menuEvent: DashboardClicked()

in NavModel class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM