Empty state page and Navigator issues?

走远了吗. 提交于 2020-01-05 03:55:09

问题


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

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

I try implement with:

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();
      }

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

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

Error is thrown:

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?

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.

Thanks!

UPDATE:

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.

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


回答1:


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

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

}


来源:https://stackoverflow.com/questions/54902636/empty-state-page-and-navigator-issues

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