red_clan_network 0.0.2 copy "red_clan_network: ^0.0.2" to clipboard
red_clan_network: ^0.0.2 copied to clipboard

API integration on next level.

Red Clan Network #

About #

red_clan_network is a Dart package designed to simplify API requests by abstracting common functionalities like GET, POST, PUT, PATCH, and DELETE methods. It provides a flexible and customizable way to handle different types of HTTP requests and parse responses into Dart models.

Features #

  • API Type: Just provide API type(GET, POST, PATCH, PUT, DELETE).
  • Flexible Data Handling: If you also need something in return you can provide model or function will return response.statusCode and resonponse.body automatically.
  • Simplified Code: Get auto mapped models in simpliest form of success or failure.

Red_Clan_Networ Example #

Create Model
class TodoModel {
  int? userId;
  int? id;
  String? title;
  bool? completed;

  TodoModel({this.userId, this.id, this.title, this.completed});

  TodoModel.fromJson(Map<String, dynamic> json) {
    userId = json['userId'];
    id = json['id'];
    title = json['title'];
    completed = json['completed'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userId'] = this.userId;
    data['id'] = this.id;
    data['title'] = this.title;
    data['completed'] = this.completed;
    return data;
  }
}
// Then add this use this function of package
Make an instance of api before use
  final ApiService apiService = ApiService();
  
//
List<TodoModel> dataList = [];
String message = 'Press button any of above';
 Future<void> fetchData() async {
    var response = await apiService.request<TodoModel>(
      url: 'https://jsonplaceholder.typicode.com/todos', 
      method: 'GET',
      modelFromJson: (json) => TodoModel.fromJson(json),
      successStatusCodes: [200, 201], 
    );
    setState(() {
      if (response.isSuccess) {
        message = 'Data Fetched Success';
      } else {
        message = 'Some thing went wrong with ${response.code}';
      }
    });
  }
  // you can call any API of get using this function

Post Example #

Future<void> postData() async {
  var bodyData = {
    "email": "codered@youtube.com",
    "secure_pin": "Asd@01814"
  };
  var response = await apiService.request(
    url: 'https://api.yoururl.com//login', // Adjusted URL
    method: 'POST',
    successStatusCodes: [200, 201], 
    headers: {
      'Content-Type': 'application/json',
    },
    body: bodyData,
  );

  setState(() {
    if (response.isSuccess) {
     
      message = 'Data post Success';
    } else {
      message = 'Something went wrong with ${response.code}';
    }
  });
}

Delete Example #

Future<void> delete() async {
  var bodyData = {
    "email": "coderedclan@youtube.com",
    "secure_pin": "Asd@01814"
  };
  var response = await apiService.request(
    url: 'https://dummy.restapiexample.com/api/v1/delete/3', // Adjusted URL
    method: 'DELETE',
    successStatusCodes: [200, 201], 
    headers: {
      'Content-Type': 'application/json',
      'Authorization':""
    },
    body: bodyData,
  );

  setState(() {
    dataList = [];
    if (response.isSuccess) {
      message = 'Delete Success';
    } else {
      message = 'Something went wrong with ${response.code}';
    }
  });
}

Put Example #

Future<void> putData()async{
var bodyData = {
  "name": "CompanyX",
  "metric_limit": 50,
  "allow_negative_credits": false
};
  var response = await apiService.request(
    url: 'https://dummy.restapiexample.com/api/v1/update/21', // Adjusted URL
    method: 'PUT',
    successStatusCodes: [200, 201], 
    headers:{
  'Authorization': ' ',
  'Content-Type': 'application/json'
},
    body: bodyData,
  );

  setState(() {
    dataList = [];
    if (response.isSuccess) {
      message = 'Data Put Success';
    } else {
      message = 'Something went wrong with ${response.code}';
    }
  });
  
}

Patch Example #

Future<void> patchData()async{
var bodyData = {
  "name": "CompanyX",
  "metric_limit": 50,
  "allow_negative_credits": false
};
  var response = await apiService.request(
    url: 'https://your_patch_api_link', // Adjusted URL
    method: 'PATCH',
    successStatusCodes: [200, 201], 
    headers:{
  'Authorization': ' ',
  'Content-Type': 'application/json'
},
    body: bodyData,
  );

  setState(() {
    dataList = [];
    if (response.isSuccess) {
      message = 'Patch Success';
    } else {
      message = 'Something went wrong with ${response.code}';
    }
  });
  
}

Learn More

Contributors #

Learn More Learn More

  • Rana Sharjeel Ali Flutter Developer Learn More
  • Muhammad Abuzar Backend Developer
3
likes
0
points
56
downloads

Publisher

unverified uploader

Weekly Downloads

API integration on next level.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, http

More

Packages that depend on red_clan_network