Created
February 10, 2020 13:24
-
-
Save robsonsilv4/be653094ea3dc3b7bb6e3bf7dc407cb5 to your computer and use it in GitHub Desktop.
Flutter consume API parsing JSON | Consumindo API e convertendo JSON
This file contains hidden or 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 '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