Viewing version 1.0.0-beta.1. Open the latest package page.

listen

1.0.0-beta.1

A package to notify state changes to interested listeners in pure Dart.

Download archive

Readme

listen

A package to notify state changes to interested listeners in pure Dart.

Usage

Using ValueNotifier

ValueNotifier wraps a single value and notifies listeners whenever the value changes:

void valueNotifierExample() {
  final counter = ValueNotifier<int>(0);

  counter.addListener(() {
    print('Value changed: ${counter.value}');
  });

  counter.value = 5; // Prints: Value changed: 5
  counter.value = 10; // Prints: Value changed: 10
  counter.value = 10; // Does not print because the value is == 10

  counter.dispose();
}

Using ChangeNotifier

Extend or mix in ChangeNotifier to manage state and notify listeners manually:

/// A [ChangeNotifier] subclass that encapsulates a list of items and notifies
/// listeners whenever items are added or removed.
class ItemListNotifier extends ChangeNotifier {
  final List<String> _items = <String>[];

  /// The unmodifiable list of current items.
  List<String> get items => List<String>.unmodifiable(_items);

  /// Adds an [item] to the list and notifies listeners.
  void addItem(String item) {
    _items.add(item);
    notifyListeners();
  }

  /// Removes an [item] from the list and notifies listeners if it was present.
  void removeItem(String item) {
    if (_items.remove(item)) {
      notifyListeners();
    }
  }
}

Then, listen to changes and update state:

void changeNotifierExample() {
  final listNotifier = ItemListNotifier();

  listNotifier.addListener(() {
    print('Current items: ${listNotifier.items}');
  });

  listNotifier.addItem('Apple'); // Prints: Current items: [Apple]
  listNotifier.addItem('Banana'); // Prints: Current items: [Apple, Banana]
  listNotifier.removeItem('Apple'); // Prints: Current items: [Banana]

  listNotifier.dispose();
}

Merging listenables

Use Listenable.merge to listen to multiple objects simultaneously:

void mergeExample() {
  final first = ValueNotifier<String>('Hello');
  final second = ValueNotifier<String>('World');

  final merged = Listenable.merge(<Listenable>[first, second]);

  merged.addListener(() {
    print('Merged listenable triggered: ${first.value} ${second.value}');
  });

  first.value = 'Hi'; // Prints: Merged listenable triggered: Hi World
  second.value = 'Dart'; // Prints: Merged listenable triggered: Hi Dart

  first.dispose();
  second.dispose();
}
Changelog

1.0.0-beta.1

  • Adjusts source code to be publishable.
  • Moves source code from flutter/flutter to flutter/core-packages.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'listen:1.0.0-beta.1'
pubspec.yaml
# Copyright 2013 The Flutter Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
name: listen
description: A package to notify state changes to interested listeners in pure Dart.
repository: https://github.com/flutter/core-packages/tree/main/packages/listen
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+listen%22
version: 1.0.0-beta.1

environment:
  sdk: ^3.10.0

dependencies:
  meta: ^1.11.0

dev_dependencies:
  test: ^1.25.9

topics:
  - state-management
  - listenable

Version history

VersionPublishedArchiveSHA-256
1.0.0-beta.0Cached · Upstream SHA-256 matchedDownloadc5a4c892…974667
1.0.0-beta.1Cached · Upstream SHA-256 matchedDownloada7c7bcc9…bfad5f
1.0.0-beta.2Cached · Upstream SHA-256 matchedDownload348da7e9…da3af1
1.0.0-beta.3Cached · Upstream SHA-256 matchedDownload40cfdfa7…cc1dd6
1.0.0-beta.4Cached · Upstream SHA-256 matchedDownloadcb0ad74d…85d085
1.0.0Cached · Upstream SHA-256 matchedDownload703154e5…c57131
1.0.1Cached · Upstream SHA-256 matchedDownload47501a08…c9e1f2

Dependencies

meta "^1.11.0"