empty_collections 2.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 38

empty_collections Build Status

The package empty_collections contains const implementations for several collection classes that allow access to there remove-functionality:

const EmptyIterator, const EmptyIterable, const EmptySet, const EmptyMap, const EmptyList, const EmptyLinkedList and const EmptyQueue.

All these classes have been implemented as permanently empty collection classes with a const constructor. This makes these classes highly efficient and e.g. usable as default arguments for optional parameters. The difference from other unmodifiable implementations (e.g. const [], const {}, const UnmodifiableSetView.empty() from the collection package) is that these classes don't adhere to the unmodifiable convention that disallows both: adding methodes (like add(...), insert(...), putIfAbsent(...), []=, ...) and removing methodes (like remove(...), clear(), retainWhere(...), ...). The thing is for empty collections the normal remove semantic doesn't change the state of the collection! E.g. a call to emptySet.clear() does nothing on an empty set, a call to emptyList.remove(o) will just return false, because o wasn't an element of the empty list to start with.

So there is no harm in allowing remove-functionality while the adding-functionlity is still of limits.

Usage #

A simple usage example:

import 'package:empty_collections/empty_collections.dart';

main() {
  // use empty collection as default values
  var args = const EmptyList();
  if(userProvidesArgs()) {
    args = getUserProvidedArgs();
  // remove-functionality doesn't throw exceptions!
  // in this specific case remove returns false
  // because "stupid option" wasn't an element of the empty list to start with
  args.remove("stupid option");
  // if you want to make sure you can add elements at some point, use toList(growable: true)
   args = args.toList(growable: true);
   args.add("important option");

// use empty collections as default values for optional parameters
void someMethodeWithOptionParameter([Map<String, String> environmentVariables = const EmptyMap, bool ignoreEnv = true]) {
  if(ignoreEnv) {
    // empty collections can be cleared without problems (because they are already empty to start with)

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

2.0.0 #

  • Dart2-ready

1.0.0 #

  • Initial version supports const contructors for EmptyIterator, EmptyIterable, EmptySet, EmptyMap, EmptyList, EmptyLinkedList and EmptyQueue


// Copyright (c) 2016, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:empty_collections/empty_collections.dart';

main() {
  // so basically the EmptyCollection classes work as expected as long
  // as you don't try to add stuff.
  // If that's the case they are more efficient than normal empty collection
  // instances (e.g. []), because there exists only one compile-time singelton
  // instance and which you can't use as default parameter values.
  // At the same time they are more permissive than unmodifiable versions
  // (e.g. const []) in that they allow remove-functionality to be invoked.
  bool wantEnemies = false;
  Set<String> enemyNames = getEnemiesNames();

  if(!wantEnemies) {
    // wouldn't work with an "unmodifiable"-style set

  print("all my enemies: $enemyNames");

// use of a const EmptyMap as default argument
Set<String> getEnemiesNames({Map<String, String> env: const EmptyMap()}) {
  // Throw away one optinal parameter. [env] may or may not contain it.
  // This wouldn't work with a const {} which by adhering to
  // the "unmodifiable"-contract would throw an exception.

  if(env.containsKey("serverUrl")) {
    return getEnemiesNamesFromServer(env["serverUrl"]);
  return const EmptySet();

Set<String> getEnemiesNamesFromServer(String serverUrl) {
  // simulate a server request
  return ["Derek", "Ben", "Gillian"].toSet();

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  empty_collections: ^2.0.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:empty_collections/empty_collections.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 20, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/src/empty_collections_base.dart. (-8.17 points)

Analysis of lib/src/empty_collections_base.dart reported 17 hints, including:

line 13 col 9: Unnecessary new keyword.

line 20 col 9: Unnecessary new keyword.

line 27 col 9: Unnecessary new keyword.

line 35 col 9: Unnecessary new keyword.

line 195 col 32: Use = to separate a named parameter from its default value.

Maintenance suggestions

Package is getting outdated. (-48.49 points)

The package was last published 77 weeks ago.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >1.24.3 <3.0.0
Dev dependencies
test >=1.3.0 <1.4.0