Last active
June 26, 2020 17:31
-
-
Save kauemurakami/c8514d795de489632f51d162bacd69b9 to your computer and use it in GitHub Desktop.
Provedor de dados
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 'dart:convert'; | |
import 'package:getx_pattern/app/data/model/model.dart'; | |
import 'package:http/http.dart' as http; | |
import 'package:meta/meta.dart'; | |
//nossa url base | |
const baseUrl = 'https://jsonplaceholder.typicode.com/posts/'; | |
//nossa classe responsável por encapsular os métodos http | |
class MyApiClient { | |
//seu client http, pode ser http, http.Client, dio, apenas traga seus métodos para cá e funcionarão normalmente :D | |
final http.Client httpClient; | |
MyApiClient({@required this.httpClient}); | |
//um exemplo rápido, aqui estamos recuperando todos os posts disponibilizados pela api(100) | |
getAll() async { | |
try { | |
var response = await httpClient.get(baseUrl); | |
if(response.statusCode == 200){ | |
Iterable jsonResponse = json.decode(response.body); | |
List<MyModel> listMyModel = jsonResponse.map((model) => MyModel.fromJson(model)).toList(); | |
return listMyModel; | |
}else print ('erro'); | |
} catch(_){ } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment