Last active
September 12, 2020 11:02
-
-
Save 6220119/b63fd5b7c1c99c35881dda0744eaf54f to your computer and use it in GitHub Desktop.
Dart type info at runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
const sampleMap = <String, dynamic>{ | |
'key': 'value', | |
'anotherKey': 6220119, | |
}; | |
print(sampleMap is Map); | |
// [output]: true | |
print(sampleMap is Map<dynamic, dynamic>); | |
// [output]: true | |
print(sampleMap is Map<String, dynamic>); | |
// [output]: true | |
const anotherMap = <dynamic, dynamic>{ | |
'key': 'value', | |
'anotherKey': 6220120, | |
}; | |
print(anotherMap is Map); | |
// [output]: true | |
print(anotherMap is Map<dynamic, dynamic>); | |
// [output]: true | |
print(anotherMap is Map<String, dynamic>); | |
// [output]: false | |
try { | |
print(anotherMap as Map<String, dynamic>); | |
} catch (error) { | |
print(error); | |
} | |
// [output]: TypeError: | |
// Instance of 'ConstantStringMap<dynamic, dynamic>': | |
// type 'ConstantStringMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment