Reputation: 1377
Even the official tutorial uses Map<String, dynamic>
. I read somewhere that dynamic
basically shuts down syntax checking and should be avoided. And in this case, JSON shouldn't contain any functions so it's safe to use Map<String, Object?>
I believe. But why it's not widely used to represent JSON data?
Upvotes: 0
Views: 429
Reputation: 9206
Using Map<String, Object?>
makes it harder to work with data represented with Map
and extract it. When you do have many nested data, you will almost have type errors, which will require you to enforce casting types using cast<T>()
, and as
, and this can make the code have more boilerplate and mess which makes the code harder to read and a simple cast as example error will crash your whole code (app).
However, If you need more type safety, and to ensure the types are correct before moving to the next steps, it's your choice to use it.
Using Map<String, dynamic>
, as the dynamic
stands for, you can make the values be of any type and the Map<String, dynamic>
will accept all of the types at that moment, then specifying the type of each one will be up to your use in your code, many Dart libraries such as json_serializable
, built_runner
.. requires simply to use Map<String, dynamic>
and will make the serialization/deserialization os JSON easier and safer.
Upvotes: 1