flutter yield does not work for same class in NavigationEvents

末鹿安然 提交于 2020-06-17 06:20:30

问题


I am trying to use the drawer to navigate (yield) to the same class (CategoryListPage) with different parameters it doesnt seem to update the widget page.

but if I go HomePage then yield CategoryListPage(), it works.

What can I do to navigate to the same class, is there a way to refresh the content ?

here is my code:

class NavigationBloc extends Bloc<NavigationEvents, NavigationStates> {
  @override
  NavigationStates get initialState => HomePage();

  @override
  Stream<NavigationStates> mapEventToState(NavigationEvents event) async* {
    switch (event) {
      case NavigationEvents.HomePageClickedEvent:
        yield HomePage();
        break;
      case NavigationEvents.ClickedEvent1:
        yield CategoryListPage(languageObjectList.list_1, languageCategoryList.languagecategorylist[1-1].category_name);
        break;
      case NavigationEvents.ClickedEvent2:
        yield CategoryListPage(languageObjectList.list_2, languageCategoryList.languagecategorylist[2-1].category_name);
        break;

some more code here:

class CategoryListPage extends StatefulWidget with NavigationStates {
...

class CategoryListPage extends StatefulWidget with NavigationStates {

List _languagelistcategorydata; String _titleappbar;

CategoryListPage(List languagelistcategorydata, String titleappbar) { _languagelistcategorydata = languagelistcategorydata; _titleappbar = titleappbar; }

here is the next page it is trying to yield(display). its a listview page.

class CategoryListPage extends StatefulWidget with NavigationStates {

  List<LanguageObject> _languagelistcategorydata;
  String _titleappbar;    

  CategoryListPage(List<LanguageObject> languagelistcategorydata, String titleappbar) {
    _languagelistcategorydata = languagelistcategorydata;
    _titleappbar = titleappbar;
  }
      @override
      _CategoryListPageState createState() => _CategoryListPageState(_languagelistcategorydata, _titleappbar);
    }

    class _CategoryListPageState extends State<CategoryListPage> {
      List<LanguageObject> items;
      String titleappbar;

  _CategoryListPageState(List<LanguageObject> languagelistcategorydata, String titleappbar) {
    this.items = languagelistcategorydata;
    this.titleappbar = titleappbar;
  }

回答1:


In your Bloc, you might have defined the Events and States class.

Are you implementing equatable?

You need to define how you want to compare the two same states that might be coming in.

For example, you can have a State Class called DisplayData() but if you will yield DisplayData() again and again, the UI might not refresh as it just checks that the last state type is DisplayData and the new state is also DisplayData hence no changes are there.

With equatable package, you specify how to compare two same State classes.

    class DisplayData extends Equatable {
      String data;
      DisplayData(this.data);

      // This is what you need to add, here you specify what fields of this state class needs to be checked for equality.
      @override
      List<Object> get props => [data];
    }

The equatable is equivalent of the Comparator class in Java.



来源:https://stackoverflow.com/questions/62132532/flutter-yield-does-not-work-for-same-class-in-navigationevents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!