Skip to content

Instantly share code, notes, and snippets.

@M001T
Created April 15, 2021 18:32
Show Gist options
  • Save M001T/dfd02e83bd25ac8622c130766f0174c7 to your computer and use it in GitHub Desktop.
Save M001T/dfd02e83bd25ac8622c130766f0174c7 to your computer and use it in GitHub Desktop.
Usamos o modelo MVC
import 'dart:convert';
import 'package:facil_alimentos/api/services_api/cache_management.dart';
import 'package:facil_alimentos/models/anuncios_model.dart';
import 'package:http/http.dart' as http;
class GetAnunciosApi {
int qtdPatronizedPref = 0;
int qtdPatronized = 0;
int qtdNotPatronized = 0;
int qtdLastPosts = 0;
Future<List<AnuncioModel>> getMyAnuncios(String userId) async {
List<AnuncioModel> anuncios = [];
var url =
'xxxxxxxxxxxxxxxxxxxxxxxxxx';
var header = {
"Content-type": "application/json",
"key": "xxxxxxxxxxxxxxxx",
"token": "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
};
Map params = {
"user_id": int.parse(userId),
};
var _body = json.encode(params);
try {
var response = await http.post(url, headers: header, body: _body);
final data = json.decode(response.body);
if (data['status'] == '400') {
return anuncios;
}
for (Map map in data['data']) {
AnuncioModel anuncio = AnuncioModel.fromJson(map);
anuncios.add(anuncio);
}
return anuncios;
} catch (e) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment