Skip to content

Instantly share code, notes, and snippets.

@robsonsilv4
Created February 10, 2020 13:24
Show Gist options
  • Save robsonsilv4/be653094ea3dc3b7bb6e3bf7dc407cb5 to your computer and use it in GitHub Desktop.
Save robsonsilv4/be653094ea3dc3b7bb6e3bf7dc407cb5 to your computer and use it in GitHub Desktop.
Flutter consume API parsing JSON | Consumindo API e convertendo JSON
import 'package:http/http.dart' as http;
// T é sua classe
// T is your class
Future<List<T>> getDeputados() async {
final response = await http.get('url');
if (response.statusCode == 200) {
final jsonDecoded =
json.decode(response.body).cast<Map<String, dynamic>>();
return jsonDecoded
.map<T>((x) => T.fromJson(x))
.toList();
} else {
throw Exception('Erro ao carregar T.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment