youtube-course-bloc icon indicating copy to clipboard operation
youtube-course-bloc copied to clipboard

"Do not use BuildContexts across async gaps" in views/main_popup_menu_button.dart

Open davideroffo opened this issue 2 years ago • 1 comments

It seems that recently flutter lint has been updated with a new linting rule that warns the developer to not use the context in async functions after an await has been performed: Use Build Context Synchronously.

image

In step-8 we are using it in the views/main_popup_menu_button.dart when showing the logout and delete account dialogs for dispatching the action to the bloc:

  • https://github.com/vandadnp/youtube-course-bloc/blob/main/lib/views/main_popup_menu_button.dart#L21
  • https://github.com/vandadnp/youtube-course-bloc/blob/main/lib/views/main_popup_menu_button.dart#L29

Some solutions are suggesting to check whether or not the component is mounted before dispatching the event, but I was wondering if there is another solution with still using the StatelessWidget, without the need of converting it into a StatefulWidget.

davideroffo avatar Jan 01 '23 06:01 davideroffo

yeah, please write this line before using context in the async method:

if(!mounted) return; context.read<...>(...);

mateendev3 avatar Jan 22 '23 22:01 mateendev3