Skip to content

Instantly share code, notes, and snippets.

@guilhermecarvalhocarneiro
Created July 23, 2019 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermecarvalhocarneiro/393863186a9bb68c3a02873dade83465 to your computer and use it in GitHub Desktop.
Save guilhermecarvalhocarneiro/393863186a9bb68c3a02873dade83465 to your computer and use it in GitHub Desktop.
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