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

1

5

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?

Profanity answered 4/9, 2018 at 13:50 Comment(3)
Please post the codeGandzha
Did the accepted answer fix your problem?Saros
Yes, it worked like a charm :)Profanity
O
8

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();
  }
}
Othella answered 4/9, 2018 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.