listenable_stream 1.1.0 copy "listenable_stream: ^1.1.0" to clipboard
listenable_stream: ^1.1.0 copied to clipboard

Convert Listenable (eg. ChangeNotifier), ValueListenable(eg. ValueNotifier) to Stream / ValueStream.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:listenable_stream/listenable_stream.dart';
import 'package:rxdart/rxdart.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'listenable_stream example',
      theme: ThemeData.dark(),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('listenable_stream example'),
      ),
      body: Center(
        child: ElevatedButton.icon(
          label: Text('GO'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => MainPage()),
            );
          },
          icon: Icon(Icons.home),
        ),
      ),
    );
  }
}

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  final controller = TextEditingController();
  late final StreamSubscription<String> subscription;

  @override
  void initState() {
    super.initState();
    subscription = controller
        .toValueStream(replayValue: true)
        .map((event) => event.text)
        .debounceTime(const Duration(milliseconds: 500))
        .where((s) => s.isNotEmpty)
        .distinct()
        .switchMap((value) => Stream.periodic(
            const Duration(milliseconds: 500), (i) => '$value..$i'))
        .listen(print);
  }

  @override
  void dispose() {
    subscription.cancel();
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Main'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            TextField(
              controller: controller,
              decoration: const InputDecoration(filled: true),
            ),
          ],
        ),
      ),
    );
  }
}
9
likes
150
pub points
85%
popularity

Publisher

unverified uploader

Convert Listenable (eg. ChangeNotifier), ValueListenable(eg. ValueNotifier) to Stream / ValueStream.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, rxdart

More

Packages that depend on listenable_stream