getValue<T> method
T?
getValue<T>()
Returns the underlying value of the SchemaDocumentValue as the provided T
type. If the value cannot be cast to the provided type, or the provided T
doesnt match Kind, then null is returned.
final doc = SchemaDocument();
String? name = "";
for (final field in doc.fields) {
if(field.name == 'name') {
name = field.getValue<String>(); // returns the value of the field 'name' as a String
}
}
if (name != null) {
print(name); // prints the value of the field 'name'
}
Implementation
T? getValue<T>() {
switch (kind) {
case Kind.BOOL:
if (T is bool) {
return boolValue.value as T;
}
break;
case Kind.INT:
if (T is int) {
return intValue.value as T;
}
break;
case Kind.FLOAT:
if (T is double) {
return floatValue.value as T;
}
break;
case Kind.STRING:
if (T is String) {
return stringValue.value as T;
}
break;
case Kind.BYTES:
if (T is Uint8List) {
return bytesValue.value as T;
}
break;
case Kind.LIST:
if (T is List) {
return listValue.value as T;
}
break;
case Kind.LINK:
if (T is String) {
return linkValue.value as T;
}
break;
default:
break;
}
return null;
}