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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With