toJson method
Parses a JSON string to a Map<String,dynamic>.
Returns null if the string isn't valid JSON:
final data = jsonString.toJson();
if (data != null) {
// Process valid JSON
}
Implementation
Map<String, dynamic>? toJson() {
try {
final dynamic decoded = jsonDecode(this);
if (decoded is Map<String, dynamic>) {
return decoded;
} else if (decoded is Map) {
return Map<String, dynamic>.from(decoded);
}
return null;
} catch (e) {
return null;
}
}