Flutter : stream et Rx

Flutter provides support for Reactive Extensions (Rx) through the rxdart package, which allows you to use observables and streams in your application.

rxdart provides a set of classes that extend the core Flutter Stream classes to provide additional functionality for working with streams in a reactive programming style. Some of the classes provided by rxdart include BehaviorSubject, PublishSubject, and ReplaySubject, which allow you to create different types of observables.

Here’s an example of how to use rxdart in Flutter to create a Stream that emits a list of integers:

import 'package:rxdart/rxdart.dart';

void main() {
  final streamController = BehaviorSubject<List<int>>();
  final stream = streamController.stream;

  stream.listen((list) {

  streamController.add([1, 2, 3]);
  streamController.add([4, 5, 6]);
  streamController.add([7, 8, 9]);

Exemple plus complet de dialogue

import 'package:rxdart/rxdart.dart';

class MyWidget extends StatefulWidget {
  _MyWidgetState createState() => _MyWidgetState();

class _MyWidgetState extends State<MyWidget> {
  final _controller = BehaviorSubject<String>();

  void dispose() {

  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StreamBuilder<String>(
          stream: _controller.stream,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Text('Received data: ${snapshot.data}');
            } else {
              return Text('No data received yet');
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _controller.add('Hello, world!');
        child: Icon(Icons.send),

Références :


Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
%d blogueurs aiment cette page :