Estou consumindo uma api em uma listview mas quando realizo um delete a lista não é atualiza.
OBS: O item selecionado para delete é apagado do DB.
Segue os detalhes:
main.dart:
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<Album>> fetchData() async {
final response = await http.get(Uri.parse('http://localhost:3000/alunos'));
if (response.statusCode == 200) {
List jsonResponse = json.decode(response.body);
return jsonResponse.map((album) => new Album.fromJson(album)).toList();
} else {
throw Exception('Unexpected error occured!');
}
}
Future<Album> deleteAlbum(String codigo) async {
final response = await http.delete(
Uri.parse('http://localhost:3000/alunos/$codigo'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
if (response.statusCode == 200) {
return Album.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a "200 OK response",
// then throw an exception.
throw Exception('Failed to delete album.');
}
}
Future<Album> updateAlbum(String title) async {
final response = await http.put(
Uri.parse('http://localhost:3000/alunos/1'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'title': title,
}),
);
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return Album.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to update album.');
}
}
class Album {
final int? codigo;
final String? nome;
Album({this.codigo, this.nome});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
codigo: json['codigo'],
nome: json['nome'],
);
}
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
final TextEditingController _controller = TextEditingController();
late Future<List<Album>> _futureData;
@override
void initState() {
super.initState();
_futureData = fetchData();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Delete Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Delete Data Example'),
),
body: Center(
child: FutureBuilder<List<Album>>(
future: _futureData,
builder: (context, snapshot) {
// If the connection is done,
// check for response data or an error.
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
List<Album>? data = snapshot.data;
return ListView.builder(
itemCount: data!.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: ListTile(
title: Text(data[index].nome.toString()),
subtitle: Text(data[index].codigo.toString()),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(
Icons.edit,
size: 20.0,
color: Colors.brown[900],
),
onPressed: () {},
),
IconButton(
icon: Icon(
Icons.delete_outline,
size: 20.0,
color: Colors.brown[900],
),
onPressed: () {
setState(() {
deleteAlbum(
data[index].codigo.toString());
});
},
),
],
),
),
);
});
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
),
),
),
);
}
}
Qualquer ajuda será bem vinda, obrigado!!