A build function returned null The offending widget is: StreamBuilder<Response>

喜欢而已 提交于 2019-12-06 07:34:35

If you don't need to render anything, don't use StreamBuilder to begin with. StreamBuilder is a helper widget used to display the content of a Stream.

What you want is different. Therefore you can simply listen to the Stream manually.

The following will do:

class Foo<T> extends StatefulWidget {
  Stream<T> stream;

  Foo({this.stream});

  @override
  _FooState createState() => _FooState<T>();
}

class _FooState<T> extends State<Foo<T>> {
  StreamSubscription streamSubscription;

  @override
  void initState() {
    streamSubscription = widget.stream.listen(onNewValue);
    super.initState();
  }

  void onNewValue(T event) {
    Navigator.of(context).pushNamed("my/new/route");
  }


  @override
  void dispose() {
    streamSubscription.cancel();
    super.dispose();
  }

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