Created
September 27, 2019 15:03
-
-
Save passsy/c041b138e0951cbb6ee0a7133d39f41c to your computer and use it in GitHub Desktop.
Sample how to use deep_pick to parse json in dart
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'; | |
import 'package:deep_pick/deep_pick.dart'; | |
import 'package:http/http.dart' as http; | |
Future<void> main() async { | |
/// Request data from a json API | |
final response = await http.get("https://pokeapi.co/api/v2/pokemon/1"); | |
final json = jsonDecode(response.body); | |
/// Pick individual field from response | |
final name = pick(json, 'name').asString(); | |
final primaryType = pick(json, 'types', 0, 'type', 'name').asStringOrNull(); | |
print("Pokemon #1 $name (type $primaryType)"); | |
// Pokemon #1 bulbasaur (type poison) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment