4.6. Advanced Dart Concepts: Streams and Blocks

Página 44

Streams and Blocs are advanced Dart concepts that allow you to manipulate data asynchronously and reactively, respectively. These concepts are fundamental to building complex and efficient applications, and are used extensively in Flutter.

Streams

In Dart, a Stream is a sequence of asynchronous events. It can emit zero or more data events, and one error or completion event. Data events are emitted sequentially and can be consumed by a listener, which is a function that processes events as they occur.

A Stream can be created in several ways. The simplest way is to use the Stream.fromIterable function, which creates a Stream from an iterable collection. For example:

var numbers = [1, 2, 3, 4, 5];
var stream = Stream.fromIterable(numbers);
stream.listen((number) {
  print(number);
});

This code creates a Stream that outputs the numbers 1 to 5, one at a time. The listen function is used to register a listener that prints out each number as it is issued.

Streams can also be created from asynchronous sources of data, such as files or network connections. For example, the File class on Dart has an openRead method that returns a Stream of bytes. This method can be used to read a file asynchronously, as in this example:

var file = File('file.txt');
var stream = file.openRead();
stream.listen((date) {
  print(String.fromCharCodes(data));
});

This code reads a file named 'file.txt' and prints its contents as text. The Stream emits data events as bytes are read from the file, and the listener converts them to text and prints them.

Blocks

In Flutter, the Bloc (Business Logic Component) pattern is used to separate the business logic from the UI. A Bloc is a class that exposes a Stream of states and accepts events. Business logic is implemented as a function that maps events to states.

For example, consider a to-do list application. The UI can emit events like 'add task' or 'remove task', and the application state can be the current list of tasks. A Bloc for this application can be implemented as follows:

class TodoBloc {
  final _stateController = StreamController>();
  final _eventController = StreamController();

  Stream> get stateStream => _stateController.stream;
  Sink get eventSink => _eventController.sink;

  AllBlock() {
    _eventController.stream.listen(_mapEventToState);
  }

  void _mapEventToState(Event event) {
    // implement business logic here
  }

  void dispose() {
    _stateController.close();
    _eventController.close();
  }
}

This code defines a Bloc that has a Stream of states and a Sink of events. Sink is an interface that allows you to add events to the Stream. The _mapEventToState method is called for each event, and is responsible for updating the state based on the event.

In summary, Streams and Blocs are powerful Dart concepts that allow you to create reactive and efficient applications. Streams let you manipulate data asynchronously, while Blocs let you separate business logic from the UI. Both are fundamental to building complex applications with Flutter.

Now answer the exercise about the content:

What are the characteristics and functions of Streams and Blocs in Dart and Flutter?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

454.7. Advanced Dart Concepts: Unit and Integration Testing

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text