Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart - How to force map key to be contained in a list strings

Let's say I have the following map that has key1 and key2:

Map<String, bool> _map = {
  'dog': true,
  'cat': true
};

Now, I want to enforce Dart to allow only a certain of keys. In Typescript, I could do something like:

enum AvailableKeysEnum {
  Dog = 'dog',
  Cat = 'cat'
}

interface AvailableKeys {
  [key in AvailableKeysEnum]: boolean;
}

const availableKeys: AvailableKeys = {
  'dog': true, // Allowed
  'cat': true, // Allowed
  'dolphin': true // Denied
}

My question is, how can I enforce Dart to allow only a certain keys?

like image 394
Eliya Cohen Avatar asked Oct 17 '25 01:10

Eliya Cohen


1 Answers

If I understood it correctly (I don't know TypeScript)

Dart doesn't have key constraints so will have to extend the Map and implement those constraints. Here is code for that.

import 'dart:collection';

class InvalidKeyError<K> extends Error {
  final Object key;
  final Set<K> keys;

  InvalidKeyError(this.key, this.keys);

  @override
  String toString() => "InvalidKeyError: $key not found in $keys";
}

class SpecialMap<K, V> extends MapMixin<K, V> {
  final Set<K> availableKeys;

  final Map<K, V> _map = {};

  SpecialMap(this.availableKeys) : assert(availableKeys != null);

  @override
  Iterable<K> get keys => _map.keys;

  @override
  void clear() => _map.clear();

  @override
  V remove(Object key) => _map.remove(key);

  @override
  V operator [](Object key) => availableKeys.contains(key)
      ? _map[key]
      : throw InvalidKeyError(key, availableKeys);

  @override
  operator []=(K key, V value) => availableKeys.contains(key)
      ? _map[key] = value
      : throw InvalidKeyError(key, availableKeys);
}

void main() {
  final availableKeys = {"cat", "dog"};
  final map = SpecialMap<String, bool>(availableKeys);

  map["cat"] = true;
  map["dog"] = true;
  map["anything else"] = false; //will throw InvalidKeyError at runtime
}

Although you can simply constrain through type if you want. Something like this.

enum Keys {cat, dog}

void main(){
  final Map<Keys, bool> map = {};
  map[Keys.cat] = true;
  map[Keys.dog] = true;
  map["any other type"] = false; //complie time error
}
like image 177
Harsh Bhikadia Avatar answered Oct 19 '25 17:10

Harsh Bhikadia