简体   繁体   English

在颤振中使用流构建器时出现错误

[英]I am getting error while using stream builder in flutter

i am making a mobile app using flutter.我正在使用颤振制作移动应用程序。 And i am using stream builder for this screen.我正在为此屏幕使用流构建器。 I am not getting the point where i am wrong in the code.我没有明白我在代码中出错的地方。 Can you please help me in this.你能帮我解决这个问题吗? I am sharing code and screenshot for this particular row which is causing problem我正在共享此特定行的代码和屏幕截图,这导致了问题

 var timeSelected = 'Click here';

Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Text(
                              'Time Slot:',
                              style: TextStyle(color: Colors.white),
                            ),
                            Spacer(),
                            GestureDetector(
                              onTap: () {
                                _asyncInputDialog(context);
                                //_displayDialog();
                              },
                              child: StreamBuilder(stream: cartManager.getTimeSlotSelected,
                                initialData: timeSelected,
                                builder: (context, AsyncSnapshot snapshot) {
                                if (snapshot.hasData){
                                 timeShow(snapshot,);
                                }
                                else if (snapshot.hasError) {
                                  return Text(snapshot.error.toString());
                                  }
                                  return Center(
                                  child: Container(
                                  child: Text('Select time slot'),
                                  ),
                                  );
                              },)
                            ),
                          ],
                        ),

This alert dialog will show when i click on the text of row:当我单击行文本时,将显示此警报对话框:

   _asyncInputDialog(
        BuildContext context,
      ) {
        return showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
                title: Center(child: Text('Available Time Slot')),
                content: TEAlertDialogContent(),
                actions: <Widget>[
                  new FlatButton(
                    child: new Text('CANCEL'),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  )
                ],
              );
            });
      }

When i got the value from showdialog i will store the value in streamcontroller that is present in CartManager.当我从 showdialog 获得值时,我会将值存储在 CartManager 中存在的流控制器中。

 static StreamController<Timeslot> timeSlotController = BehaviorSubject();

  timeSlotSelected(Timeslot time){
    timeSlotController.sink.add(time);
  }

  get getTimeSlotSelected{
    return timeSlotController.stream;
  }

And we call the above method in stream property of streamcontroller and get the snapshot.并且我们在streamcontroller的stream属性中调用上面的方法并获取快照。 This is the method which was called when our snapshot has data:这是当我们的快照有数据时调用的方法:

  Widget timeShow(AsyncSnapshot<Timeslot> snapshot ) {
    timeSelected = '${snapshot.data.firstTimeSlot}-${snapshot.data.secondTimeSlot}';
    timeslotid = snapshot.data.id.toString();
    return Text(timeSelected);
  }

But i am getting error: type 'BehaviorSubject' is not a subtype of type 'Stream' Please let me know where i am wrong.但我收到错误: “BehaviorSubject”类型不是“Stream”类型的子类型请告诉我我错在哪里。 I had also shared a screen shot of screen showing this error too.我还分享了一个显示此错误的屏幕截图。 错误截图

As your error states, you are trying to pass a type Timeslot to a Stream builder expecting a stream of type String.正如您的错误所述,您正在尝试将类型时间段传递给需要字符串类型流的流构建器。 You must check which one is correct (String or Timeslot) and use the same type on both sides.您必须检查哪一个是正确的(字符串或时间段)并在两侧使用相同的类型。 Apparently, your problem is in the timeSelected variable.显然,您的问题出在 timeSelected 变量中。 Where is it defined?它在哪里定义? If this is a String, the Stream builder will infer that your stream is of type String, which is not true.如果这是一个字符串,流构建器将推断您的流是字符串类型,但事实并非如此。 You must set this variable as a Timeslot, since this is your stream type.您必须将此变量设置为时间段,因为这是您的流类型。

Also, you have an error in your code.此外,您的代码中有错误。 You have to return a widget to be rendered if snapshot has data.如果快照有数据,您必须返回要呈现的小部件。 Check the code below:检查下面的代码:

StreamBuilder(stream: cartManager.getTimeSlotSelected,
                            initialData: timeSelected,
                            builder: (context, AsyncSnapshot snapshot) {
                            if (snapshot.hasData){
                             return timeShow(snapshot,);
                            }
                            else if (snapshot.hasError) {
                              return Text(snapshot.error.toString());
                              }
                              return Center(
                              child: Container(
                              child: Text('Select time slot'),
                              ),
                              );
                          },)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到 Flutter 中的 mqtt 时出现错误 builder.payload - I am getting error builder.payload while connecting to mqtt in Flutter 使用 package CarouselSlider 时出现错误,它显示错误类型“图像”不是 flutter 中“字符串”类型的子类型 - I am getting error while using package CarouselSlider it display error type 'Image' is not a subtype of type 'String' in flutter Flutter Bloc-为什么我在使用 bloc 时得到错误状态作为输出, - Flutter Bloc-why i am getting error state as output while using bloc , 在运行颤振应用程序时出现此错误 - while running the flutter app i am getting this error 为什么在更新 flutter sdk 时出现错误? - Why am I getting Error while updating the flutter sdk? 没有错误,但我在使用 GetX flutter 时没有从我的 Firestore 数据库中获取数据 - There's no error but I am not getting data from my firestore database while using GetX flutter 我在尝试运行我的颤振项目时遇到错误 - I am getting error while trying to run my flutter project 为 iOS 运行 flutter 项目时出现错误 - I am getting error while running the flutter project for iOS 运行 flutter 应用程序时出现错误 - I am getting error while running my flutter app 为什么在添加广告时 Flutter 出现错误 - Why I am getting ERROR in Flutter While adding ad
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM