I am relying on the flutter documentation for this api call. but I am not rendering the images from the url on the screen, it keeps loading infinitely.
import 'package:apidog/models/dogdata.dart';
import 'package:flutter/material.dart';
// ignore: unused_import
import 'package:http/http.dart' as http;
import 'dart:convert';
class Api extends StatefulWidget {
//se convierte en un objeto dart
Future<Dogdata> fetchDogdata() async {
final response =
await http.get(Uri.parse('https://dog.ceo/api/breed/hound/images'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return Dogdata.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
//Api({Key? key}) : super(key: key);
@override
State<Api> createState() => _ApiState();
}
class _ApiState extends State<Api> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder(
future: widget.fetchDogdata(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
final lista = (snapshot.data["message"] as List);
return ListView.builder(
itemCount: lista.length,
itemBuilder: (BuildContext context, int index) {
return Image.network(lista[index]);
},
);
} else {
return CircularProgressIndicator();
}
},
),
),
);
}
}
here the external code of the model.
class Dogdata {
final String imagen;
const Dogdata({
required this.imagen,
});
//crea un archivo json
factory Dogdata.fromJson(Map<String, dynamic> json) {
return Dogdata(
imagen: json['message'],
);
}
}
I see some errors:
It would be like this:
Result: