tryGetString method

  1. @preferInline
String? tryGetString(
  1. String key, {
  2. String? defaultValue,
})

Retrieves a String value from the JSON map associated with the given key.

If the value associated with the key is already a String, it returns that value. Otherwise, it returns defaultValue.

Unlike getString, this method does not store the parsed or existing String back into the JSON map.

Returns:

  • A String if the value is valid.
  • defaultValue if the value is not a valid String.
  • null if both the value and defaultValue are null.

Implementation

@preferInline
String? tryGetString(
  String key, {
  String? defaultValue,
}) {
  final value = _json[key];
  if (value is String) {
    return value;
  }
  return defaultValue;
}