Skip to content

Instantly share code, notes, and snippets.

@guilhermecarvalhocarneiro
Created July 29, 2020 20:19
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/37d64ce602244db6ea71a7c6c7f11252 to your computer and use it in GitHub Desktop.
Save guilhermecarvalhocarneiro/37d64ce602244db6ea71a7c6c7f11252 to your computer and use it in GitHub Desktop.
class Atrativo {
int id;
String nome;
String endereco;
double latitude;
double longitude;
String pontuacao;
int curtidas;
List<String> imagens;
Atrativo(
{this.id,
this.nome,
this.endereco,
this.latitude,
this.longitude,
this.pontuacao,
this.curtidas,
this.imagens});
String getSinglePicture() {
var uri = Util.getRandomImage();
try {
if (this.imagens.length != 0) {
uri = this.imagens[0];
}
} 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);
}
uri = Util.getRandomImage();
}
return uri;
}
static Future<List<Atrativo>> getAtrativos(int cidade) async {
List<Atrativo> _atrativos = [];
if (cidade == null) {
return _atrativos;
}
try {
var dio = Dio();
Response response = await dio
.get("${Config.uri}atrativo-list/?cidade=$cidade&format=json");
if (response.statusCode == 200) {
for (var item in response.data) {
_atrativos.add(Atrativo.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 _atrativos;
}
Atrativo.fromJson(Map<String, dynamic> json) {
id = json['id'];
nome = json['nome'];
endereco = json['endereco'];
latitude = json['latitude'];
longitude = json['longitude'];
pontuacao = json['pontuacao'];
curtidas = json['curtidas'];
imagens = [];
for (var imagem in json['imagens']) {
if (imagem.contains("http://192.168.0.7:9090/media/") == true) {
imagens.add(imagem.replaceAll(
"http://192.168.0.7:9090/media/", Config.uri_media));
} else {
imagens.add("${Config.uri_media}$imagem");
}
}
// imagens = json['imagens'].cast<String>();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['nome'] = this.nome;
data['endereco'] = this.endereco;
data['latitude'] = this.latitude;
data['longitude'] = this.longitude;
data['pontuacao'] = this.pontuacao;
data['curtidas'] = this.curtidas;
data['imagens'] = this.imagens;
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment