Skip to content

Instantly share code, notes, and snippets.

@hawkkiller
Created January 21, 2024 15:19
Show Gist options
  • Save hawkkiller/3aaf63d0d8136db9ce7d9dc2d5f4bfc7 to your computer and use it in GitHub Desktop.
Save hawkkiller/3aaf63d0d8136db9ce7d9dc2d5f4bfc7 to your computer and use it in GitHub Desktop.
import 'dart:convert';
void main() {
final johnJson = '{"name": "John Doe"}';
final john = getUser(johnJson);
final wrongJson = '{"name": "John Doe}';
// this raises FormatException
final wrong = getUser(wrongJson);
}
User getUser(String json) {
// This can throw FormatException, however it's not handled
final decoded = jsonDecode(json);
final name = decoded['name'] as String;
return User(name);
}
class User {
final String name;
const User(this.name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment