Flutter delete from Jsonplaceholder not deleting

Viewed 21

in my flutter project i'm using jsonplaceholder to display it in a future builder. when i use get method it works fine but when using delete method it returns status code 200 but the results are not changing. is it b/c using fake api doesn't actually update or delete or am i doing something wrong?

model for user.

import 'dart:convert';

List<User> usersFromJson(String str) =>
    List<User>.from(json.decode(str).map((json) => User.fromJson(json)));
String usersToJson(List<User> data) =>
    json.encode(List<dynamic>.from(data.map((e) => e.toJson())));

class User {
  int? id;
  String? name;
  String? username;
  String? email;
  String? role;
  String? password;

  User(
      {this.id,
      this.name,
      this.username,
      this.email,
      this.role,
      this.password});

  @override
  toString() => 'User: $name';

  factory User.fromJson(Map<String, dynamic> json) => User(
      email: json['title'],
      name: json['title'],
      id: json['id'],
      username: json['title'],
      role: json['title']);

  Map<String, dynamic> toJson() => {
        "name": name,
        "username": username,
        "email": email,
        "role": role,
        "password": password
      };
}

and API request

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import '../user_model.dart';

Future<List> fetchUsers() async {
  Uri url = Uri.parse("https://jsonplaceholder.typicode.com/posts");
  final response = await http.get(url);
  return usersFromJson(response.body);
}

Future deleteUser(var id) async {
  Uri url = Uri.parse("https://jsonplaceholder.typicode.com/posts/$id");
  final response = await http.delete(url, headers: <String, String>{
    'Content-Type': 'application/json; charset=UTF-8',
  });
  if (response.statusCode == 200) {
    // return {
    //   usersToJson(usersFromJson(response.body)),
    //   print(".............Success..........")
    // };

    return {
      User.fromJson(jsonDecode(response.body)),
      Get.snackbar("Deleted $id", "Successfully deleted id of $id",
          duration: const Duration(seconds: 3),
          colorText: Colors.green,
          backgroundColor: Colors.white),
    };
  } else {
    return {
      Get.snackbar("not Deleted $id", "Failed to deleted id of $id",
          duration: const Duration(seconds: 3),
          colorText: Colors.red,
          backgroundColor: Colors.white)
    };
  }
}

and my screen

...
FutureBuilder(
  future: fetchUsers(),
  builder: (context, snapshot) {
       if (snapshot.hasData) {
          return Column(
             children: [
               MaterialButton(
                 onPressed: () {
                   deleteUser(id);
                   print(
                      "deleted object is $id from ${datasList[int.parse(id) - 1].name}");
                   print("all list datas are ${datasList.toList()}");
                 ...
                },
       ])}
  }
)

please i need an answer.

0 Answers
Related