diff --git a/packages/moxxmpp/lib/src/util/typed_map.dart b/packages/moxxmpp/lib/src/util/typed_map.dart index e19b4c1..f6a096e 100644 --- a/packages/moxxmpp/lib/src/util/typed_map.dart +++ b/packages/moxxmpp/lib/src/util/typed_map.dart @@ -1,5 +1,15 @@ /// A map, similar to Map, but always uses the type of the value as the key. class TypedMap { + /// Create an empty typed map. + TypedMap(); + + /// Create a typed map from a list of values. + TypedMap.fromList(List items) { + for (final item in items) { + _data[item.runtimeType] = item; + } + } + /// The internal mapping of type -> data final Map _data = {}; diff --git a/packages/moxxmpp/test/type_map_test.dart b/packages/moxxmpp/test/type_map_test.dart index 7d9e7b9..e214e56 100644 --- a/packages/moxxmpp/test/type_map_test.dart +++ b/packages/moxxmpp/test/type_map_test.dart @@ -24,4 +24,16 @@ void main() { expect(map.get()?.i, 1); expect(map.get()?.j, false); }); + + test('Test storing data in the type map using a list', () { + // Set + final map = TypedMap.fromList([ + const TestType1(1), + const TestType2(false), + ]); + + // And access + expect(map.get()?.i, 1); + expect(map.get()?.j, false); + }); }