空状态页和导航器问题? [英] Empty state page and Navigator issues?

查看:823
本文介绍了空状态页和导航器问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Flutter应用中实现空状态页面,当新闻提要为空时.

I want to implement empty state page in Flutter app, when news feed is empty.

如果Feed中没有任何活动,我想显示没有活动" .当数据库中有数据时,我想将其从StreamBuilder加载到ListView.builder

When there is no activity in feed, I want to show ‘no activity’. When database has data, I want to load this from StreamBuilder to ListView.builder

我尝试使用:

child: StreamBuilder(
    stream: collection.snapshots(),
    builder: (context, snapshot) {

    if(snapshot.hasData) {
       return ListView.builder(
            itemBuilder: (context, index) =>
                build(context, snapshot.data.documents[index]),
            itemCount: snapshot.data.documents.length,
         );
      } else {
          return _emptyStateWidget();
      }

我必须在else语句中返回某些内容,因为有时快照将没有数据,因此会引发Widget返回null的错误.

I must return something in else statement because sometimes snapshot will not have data so will throw error that Widget return null.

但这会导致小部件树出现问题.因为如果以后尝试在build小部件中使用Navigator.push,则:

But this is causing issues with widget tree. Because if I try to use Navigator.push in build Widget later:

Widget build(BuildContext context, DocumentSnapshot document) {
…
FlatButton(
  onPressed: () async {

await function(document);

if(validated == true) {
await Navigator.push(context, new MaterialPageRoute(
    builder: (BuildContext context) =>
    new Screen(documentInfo: documentInfo)));
}

引发错误:

查找已停用的窗口小部件的祖先是不安全的.在此刻 小部件的元素树的状态不再稳定.为了安全 在其dispose()方法中引用窗口小部件的祖先,保存一个引用 通过在 小部件的didChangeDependencies()方法.

Looking up a deactivated widget's ancestor is unsafe. At this point the state of the widget's element tree is no longer stable. To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

有没有办法避免这种情况,从而不会引发任何错误?

Is there way to avoid this so that no error is thrown?

由于某种原因,如果我在等待function(document)之前调用Navigator.push,则不会引发任何错误.但是我不能允许这样做,因为它必须先在功能中执行数据验证,并且只有在成功时才进行导航.

For some reason if I call Navigator.push before await function(document) there is no error thrown. But I cannot allow this because it must perform data validation in function before and only navigate if success.

谢谢!

更新:

经过更多测试,我认为问题是由StreamBuilder引起的.每当收到新事件时,它都会重建.因此,这会在调用Navigator.push并导致获得新事件时引起问题,因此请尝试重建.

After more test I think problem is cause by StreamBuilder. It rebuild whenever it get new event. So this cause issue when call Navigator.push and it get new event so try to rebuild.

如何解决?可以阻止StreamBuilder重建后代吗?

How to solve? Is possible to stop StreamBuilder from rebuild descendant?

推荐答案

之所以会发生这种情况,是因为您在构建小部件树的同时试图导航到其他地方.

That happens because you are trying to navigate to somewhere else while your widget tree is still being built.

您应将Navigation.push移至流侦听器,例如,在您的initState中.

You should move the Navigation.push to a stream listener, for example, in your initState.

void initState() {
super.initState();
collection.snapshots.listen((data){

if(validated == true) {
   await Navigator.push(context, new MaterialPageRoute(
      builder: (BuildContext context) =>
      new Screen(documentInfo: documentInfo)));
  }
});

}

这篇关于空状态页和导航器问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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