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

孤人 提交于 2019-12-10 10:29:38

问题


I'm new to Flutter and I'm trying to accomplish a simple thing: I want to create a signup functionality using BLoC pattern and streams.

For the UI part I have a stepper, that on the very last step should fire a request to the server with the collected data.

I believe I have everything working until the StreamBuilder part. StreamBuilders are meant to return Widgets, however, in my case I don't need any widgets returned, if it's a success I want to navigate to the next screen, otherwise an error will be displayed in ModalBottomSheet.
StreamBuilder is complaining that no widget is returned.

Is there anything else that could be used on the View side to act on the events from the stream?

Or is there a better approach to the problem?


回答1:


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


来源:https://stackoverflow.com/questions/52167894/a-build-function-returned-null-the-offending-widget-is-streambuilderresponse

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