生成函数返回null令人讨厌的窗口小部件是:StreamBuilder< Response>. [英] A build function returned null The offending widget is: StreamBuilder<Response>

查看:96
本文介绍了生成函数返回null令人讨厌的窗口小部件是:StreamBuilder< Response>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flutter的新手,我正在尝试完成一个简单的事情: 我想使用BLoC模式和流创建注册功能.

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.

对于UI部分,我有一个 stepper ,则在最后一步应使用收集的数据向服务器发出请求.

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.

我相信直到StreamBuilder之前,我都可以正常工作. StreamBuilders用于返回小部件,但是,在我的情况下,我不需要返回任何小部件,如果成功,我想导航到下一个屏幕,否则将在ModalBottomSheet中显示错误.
StreamBuilder抱怨没有小部件返回.

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.

在View端还可以使用其他方法来处理流中的事件吗?

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?

推荐答案

如果不需要渲染任何东西,请不要使用StreamBuilder作为开头. StreamBuilder是用于显示Stream内容的帮助小部件.

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.

您想要的与众不同.因此,您可以轻松地手动收听Stream.

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

将执行以下操作:

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

这篇关于生成函数返回null令人讨厌的窗口小部件是:StreamBuilder&lt; Response&gt;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆