颤振屈服不适用于NavigationEvents中的同一类 [英] flutter yield does not work for same class in NavigationEvents

查看:263
本文介绍了颤振屈服不适用于NavigationEvents中的同一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用抽屉来导航(屈服)具有不同参数的同一个类(CategoryListPage),但它似乎并未更新小部件页面.

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.

但是如果我进入HomePage然后产生CategoryListPage(),它将起作用.

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 ?

这是我的代码:

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;

更多代码在这里:

class CategoryListPage extends StatefulWidget with NavigationStates {
...

类别CategoryListPage用NavigationStates {

class CategoryListPage extends StatefulWidget with NavigationStates {

列出_languagelistcategorydata; 字符串_titleappbar;

List _languagelistcategorydata; String _titleappbar;

CategoryListPage(列表语言列表类别数据,字符串标题应用栏){ _languagelistcategorydata = languagelistcategorydata; _titleappbar = 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;
  }

推荐答案

在您的Bloc中,您可能已经定义了事件和状态类.

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

您要实现equatable吗?

您需要定义如何比较可能进入的两个相同状态.

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

例如,您可以有一个名为DisplayData()的状态类,但是如果要一次又一次产生DisplayData(),则UI可能不会刷新,因为它只是检查最后一个状态类型是DisplayData而新状态也是DisplayData因此没有任何变化.

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.

使用equatable包,您可以指定如何比较两个相同的State类.

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];
    }

equatable等效于Java中的Comparator类.

The equatable is equivalent of the Comparator class in Java.

这篇关于颤振屈服不适用于NavigationEvents中的同一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆