Created
January 21, 2024 15:19
-
-
Save hawkkiller/3aaf63d0d8136db9ce7d9dc2d5f4bfc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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