Drawer navigiation load content with BLoC

Issue

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?

Solution

the DashboardClicked is sub class,

change code from :

menuEvent: MenuEvent.DashboardClicked

to :

menuEvent: DashboardClicked()

in NavModel class

Answered By – farouk osama

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.