在flutter中使用流生成器时出现错误

问题描述 投票:0回答:1

我正在使用Flutter开发移动应用。我正在为此屏幕使用流生成器。我没有弄清楚代码中的错误。你能帮我吗我正在为此问题的特定行共享代码和屏幕截图

 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'),
                                  ),
                                  );
                              },)
                            ),
                          ],
                        ),

当我单击行文本时,将显示此警报对话框:

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

当我从showdialog获取值时,我会将值存储在CartManager中存在的streamcontroller中。

 static StreamController<Timeslot> timeSlotController = BehaviorSubject();

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

  get getTimeSlotSelected{
    return timeSlotController.stream;
  }

然后我们在streamcontroller的stream属性中调用上述方法并获取快照。当快照具有数据时,将调用此方法:

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

但是我遇到了错误:BehaviorSubject类型不是'Stream'类型的子类型请让我知道我错了。我也分享了显示此错误的屏幕截图。error screenshot

flutter dart stream behaviorsubject stream-builder
1个回答
0
投票

由于您的错误状态,您正在尝试将类型Timeslot传递给期望流类型为String的Stream构建器。您必须检查哪一个是正确的(字符串或时隙),并在两侧使用相同的类型。

© www.soinside.com 2019 - 2024. All rights reserved.