Skip to content

Instantly share code, notes, and snippets.

@passsy
Created September 27, 2019 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passsy/c041b138e0951cbb6ee0a7133d39f41c to your computer and use it in GitHub Desktop.
Save passsy/c041b138e0951cbb6ee0a7133d39f41c to your computer and use it in GitHub Desktop.
Sample how to use deep_pick to parse json in dart
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