Reputation: 2351
I'm flutter baby since last fall. When we use flutter types, they usually provide general features like toString(). One of them is '.runtimeType
' But this 'getting runtimeType
' method seems like not working on release mode, which is actually working well on debug mode.
Let's say that we've got normal map like Map amIMap = {"key": "is key", "value": "is value",};
Normally we think that the type of this var is 'Map', but print(amIMap.runtimeType);
method in debug mode(which doesn't seem like working on release mode) print not 'Map' but something like LinkedHashMap
, which might be in slightly different manner by dev environment but basically same concept of LinkedHashMap
.
So here comes the question. In this link 'https://api.flutter.dev/flutter/dart-collection/LinkedHashMap-class.html', it seems like the printed type from above situation is something that Dart team actually intended, and it seems really like orderedDict in python. And through this question, i want to make sure that this concept of LinkedHashMap
is safe to use. What i'm saying is that i want to know if the order of pairs with keys and their corresponding values safely remains same just like 'orderedDict' in python, REGARDLESS ON the environment like debug, profile, release mode, dartpad, and even platform like android, ios, macos, linux, window, web!
Since i've seen that runtimeType does not work well on release mode in my app, i'm afraid of that the order of the key-value pairs might be forgotten by the sdk in release mode, just like runtimeType method.
The reason why i'm asking this question is that i need to force the order of the pairs to be totally same as my dart code in IDEA, so that i can convert them into same format of json string then hashing the json strings, which all have exactly the same order of pairs, with crypto package's sha256 hashing method. The order of map must remain same before they get hashed. What makes me scared is that flutter tends to work differently environment by environment. Also when i use firestore plugin, i have to sort the map from firestore manually since they are not in the same order of the one which can be checked via firebase console. And it seems like these issues are in similar context. So i need bool value here if linkedHashMap is gonna save my maps' order of pairs even in release mode regardless on various platforms of flutter 2.0, just like orderedDict in python.
Hope anyone guru in this field answer this question with fully convinced experience. And i'd really appreciate for your comments if i left something unclear or even wrong in this question,
Thank you in advance [:
Upvotes: 3
Views: 488
Reputation: 31299
First, you should never use .runtimeType
for anything other than debugging. As you have seen, the output is really not that stable and therefore very rarely be used for anything. You should instead use the is
operator if you need to check if a given object is compatible with a given class
.
Second, the Map()
constructor (the {}
syntax does the same) in Dart is documented to be creating an instance of LinkedHashMap
. Since this is part of the standard SDK and language design, this is properly not going to change. Also, this behavior will be stable across all platforms where Dart is running.
But if you want to be sure you are always using a LinkedHashMap
, you can just explicit create an instance of LinkedHashMap
by importing the class from dart:collection
.
LinkedHashMap
is documented as:
The insertion order of keys is remembered, and keys are iterated in the order they were inserted into the map. Values are iterated in their corresponding key's order. Changing a key's value, when the key is already in the map, does not change the iteration order, but removing the key and adding it again will make it be last in the iteration order.
Upvotes: 2