Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 669
Mantoska Avatar asked Sep 04 '18 13:09

Mantoska


1 Answers

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();
  }
}
like image 52
Rémi Rousselet Avatar answered Oct 21 '22 11:10

Rémi Rousselet