DatePicker + Streams, can be done?

Viewed 270

I'm using streams to pass data between my widgets, but now I'm building a DatePicker and I've faced a problem.

The DatePicker returns Future and I don't know how to push it to my streams.

Do you people have any suggestions on how to solve this?

Thx

2 Answers

I haven't tried it for DatePicker but I think it can be changed to stream via asStream() then you can pipe it to your stream.

showDatePicker(
      context: context,
      initialDate: DateTime.now(),
    ).asStream().pipe(/*your streamcontroller*/);

Or you can use addStream.

YourStreamController.sink.addStream(showDatePicker(
      context: context,
      initialDate: DateTime.now(),
    ).asStream());

I'm using BehaviorSubject from RxDart instead of the StreamControllers, so I couldn't pass the DatePicker directly as Stream.

I'm new to flutter, so I don't know if it is the best way to solve it, but I succeeded doing this:

void _pickDate(BuildContext context) async {
 // I use this manager for my sinks and streams
 InputManager manager = Provider.of(context).fetch(InputManager);

 DateTime _date = await showDatePicker(
   context: context,
   initialDate: DateTime.now(),
   firstDate: DateTime(DateTime.now().year - 5),
   lastDate: DateTime(DateTime.now().year + 1),
 );

 if (_date != null) {
   setState(() {
     inDate = _date;
   });
 }

 manager.setDate.add(inDate);
}
Related