简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM