Answers for "delete from api in flutter"

0

delete from api in flutter

Future<Album> deleteAlbum(String id) async {
        final http.Response response = await http.delete(
          Uri.parse('https://jsonplaceholder.typicode.com/albums/$id'),
        headers: <String, String>{
       'Content-Type': 'application/json; charset=UTF-8',
           },
          );
        if (response.statusCode == 200) {
       // If the server did return a 200 OK response,
      // then parse the JSON. After deleting,
      // you'll get an empty JSON `{}` response.
      // Don't return `null`, otherwise `snapshot.hasData`
      // will always return false on `FutureBuilder`.
      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.');
        }  
       }
Posted by: Guest on April-08-2022

Browse Popular Code Answers by Language