felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.8k stars 3.39k forks source link

[question] refresh data on page pop #1110

Closed mflesh1 closed 4 years ago

mflesh1 commented 4 years ago

I am sure I am missing something.

I have a main screen that displays a data loaded from an API, this is loaded using the bloc pattern. The user then navigates (pop) to a second page.

The user then can hit the back arrow to return to the original page at which point I want to reload (hit api using bloc) the data.

I can't seem to figure out how to do this.

felangel commented 4 years ago

Hi @mflesh1 πŸ‘‹ Thanks for opening an issue!

You can await the Navigator.push and then add an event to the bloc to reload like:

await Navigator.of(context).push(...);
context.bloc<MyBloc>().add(DataRequested());

Hope that helps!

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation πŸ‘

mflesh1 commented 4 years ago

@felangel thanks for the response. I tried this but the event is not triggering the bloc, any ideas? What context is being used the BuildContext?

felangel commented 4 years ago

Hi @mflesh1 yup the BuildContext will allow you to access the bloc if you provided it via BlocProvider higher up in the tree.

Are you able to share a link to a sample app which illustrates the problem you’re facing? Thanks πŸ‘

mflesh1 commented 4 years ago

I receive this error when I try this.

E/flutter (11801): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter (11801): At this point the state of the widget's element tree is no longer stable.
E/flutter (11801): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
felangel commented 4 years ago

@mflesh1 can you share a sample app which illustrates the issue?

narcodico commented 4 years ago

@mflesh1 I suspect you're manually calling dispose where you're proving the bloc. You shouldn't do that since the bloc will be disposed for you automatically by the BlocProvider. πŸ‘

mflesh1 commented 4 years ago

@felangel it is hard to share a sample app as I can't share the app I am working on, I would need to deconstruct it a bit to share, I will see what I can do.

mflesh1 commented 4 years ago

@felangel so I added an example project, it has a home page that has a list, you can then navigate to another page, when you come back I want the home page list to refresh. The sample works on the first time you navigate but on subsequent navigations it does not.

It is implemented in this file.

_navigate(BuildContext context, String route) {
    locator<NavigationService>().navigateTo(route).then((value) {
      BlocProvider.of<ScreenBloc>(context).add(ScreenRefreshEvent());
      locator<NavigationService>().pop();
    });
  }
pharshdev commented 4 years ago

@mflesh1 try this

ScreenBloc screenbloc = BlocProvider.of<ScreenBloc>(context);

then in dispose

screenbloc.add(ScreenRefreshEvent());

@felangel why this wont work in dispose?

BlocProvider.of<ScreenBloc>(context).add(ScreenRefreshEvent());
saucelooker commented 10 months ago

Hi @mflesh1 πŸ‘‹ Thanks for opening an issue!

You can await the Navigator.push and then add an event to the bloc to reload like:

await Navigator.of(context).push(...);
context.bloc<MyBloc>().add(DataRequested());

Hope that helps!

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation πŸ‘

What if I have 4 pages? I am updating the data on the fourth page, and how can I update the data on the first page?

dipu0 commented 4 weeks ago

I receive this error when I try this.

E/flutter (11801): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter (11801): At this point the state of the widget's element tree is no longer stable.
E/flutter (11801): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

having same issues! can you share how you solved this?

dipu0 commented 4 weeks ago

Hi @mflesh1 πŸ‘‹ Thanks for opening an issue!

You can await the Navigator.push and then add an event to the bloc to reload like:

await Navigator.of(context).push(...);
context.bloc<MyBloc>().add(DataRequested());

Hope that helps!

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation πŸ‘

if i want to show data in text field then loading data after screen loaded does not work.