Skip to content

Instantly share code, notes, and snippets.

@6220119
Last active September 12, 2020 11:02
Show Gist options
  • Save 6220119/b63fd5b7c1c99c35881dda0744eaf54f to your computer and use it in GitHub Desktop.
Save 6220119/b63fd5b7c1c99c35881dda0744eaf54f to your computer and use it in GitHub Desktop.
Dart type info at runtime
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