Created
July 23, 2019 20:04
-
-
Save guilhermecarvalhocarneiro/393863186a9bb68c3a02873dade83465 to your computer and use it in GitHub Desktop.
This file contains 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:dio/dio.dart'; | |
import 'package:minhacidade/utils/config.dart'; | |
import 'package:minhacidade/utils/util.dart'; | |
class Evento { | |
int id; | |
String titulo; | |
String horario; | |
String valor; | |
String foto; | |
String descricao; | |
String nomeCidade; | |
String nomeEstabelecimento; | |
String telefone; | |
String dias; | |
Evento( | |
{this.id, | |
this.titulo, | |
this.horario, | |
this.valor, | |
this.foto, | |
this.descricao, | |
this.nomeCidade, | |
this.nomeEstabelecimento, | |
this.telefone, | |
this.dias}); | |
String getImage() { | |
return Util.getRandomImage(); | |
} | |
static Future<List<Evento>> getEventos(cidade) async { | |
List<Evento> _eventos = []; | |
try { | |
if (cidade == null){ | |
return _eventos; | |
} | |
var dio = Dio(); | |
Response response = | |
await dio.get("${Config.uri}evento-list/?cidade=$cidade&format=json"); | |
print("Recuperando eventos da API"); | |
if (response.statusCode == 200) { | |
for (var item in response.data) { | |
_eventos.add(Evento.fromJson(item)); | |
} | |
} | |
} on DioError catch (e) { | |
// The request was made and the server responded with a status code | |
// that falls out of the range of 2xx and is also not 304. | |
if (e.response != null) { | |
print(e.response.data); | |
print(e.response.headers); | |
print(e.response.request); | |
} else { | |
// Something happened in setting up or sending the request that triggered an Error | |
print(e.request); | |
print(e.message); | |
} | |
} | |
return _eventos; | |
} | |
Evento.fromJson(Map<String, dynamic> json) { | |
try { | |
id = json['id']; | |
titulo = json['titulo']; | |
horario = json['horario']; | |
valor = json['valor']; | |
foto = json['foto']; | |
descricao = json['descricao']; | |
nomeCidade = json['nome_cidade']; | |
nomeEstabelecimento = json['nome_estabelecimento']; | |
telefone = json['telefone']; | |
dias = json['dias']; | |
} catch (error) { | |
print(error); | |
print(json); | |
} | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = new Map<String, dynamic>(); | |
data['id'] = this.id; | |
data['titulo'] = this.titulo; | |
data['horario'] = this.horario; | |
data['valor'] = this.valor; | |
data['foto'] = this.foto; | |
data['descricao'] = this.descricao; | |
data['nome_cidade'] = this.nomeCidade; | |
data['nome_estabelecimento'] = this.nomeEstabelecimento; | |
data['telefone'] = this.telefone; | |
data['dias'] = this.dias; | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment