What is the difference between Sink and Stream in Flutter?

后端 未结 4 651
栀梦
栀梦 2021-02-03 23:50

The Google I/O 2018 video about Flutter explains how to use Dart streams to manage state in a Flutter application. The speaker talked about using Sink as input stre

4条回答
  •  广开言路
    2021-02-04 00:27

    Sink and Stream both are parts of the StreamController. You add a data to the StreamController using Sink which can be listened via the Stream.

    Example:

    final _user = StreamController();
    Sink get updateUser => _user.sink;
    Stream get user => _user.stream;
    

    Usage:

    updateUser.add(yourUserObject); // This will add data to the stream.
    

    Whenever a data is added to the stream via sink, it will be emitted which can be listened using the listen method.

    user.listen((user) => print(user)); 
    

    You can perform a various number of actions before the stream is emitted. transform method is an example which can be used to transform the input data before it gets emitted.

提交回复
热议问题