httpagent 0.0.7
httpagent: ^0.0.7 copied to clipboard
httpagent is a comprehensive library of custom http api calling for Flutter.
httpagent #
httpagent is a simple and easy-to-use Dart package for making HTTP requests. It supports GET, POST, PUT, DELETE, and multipart POST requests with customizable headers and request bodies.
Features #
- Perform GET, POST, PUT, DELETE, and multipart POST requests.
- Easily handle JSON data and headers.
- Callback functions to handle task completion and errors.
Installation #
Add httpagent to your pubspec.yaml file:
dependencies:
httpagent: ^0.0.3
Then run:
flutter pub get
Usage #
Import the package in your Dart file:
import 'package:httpagent/httpagent.dart';
Example #
Here's an example of how to use httpagent in a Flutter app:
import 'package:flutter/material.dart';
import 'package:httpagent/httpagent.dart';
class TestScreen extends StatefulWidget {
const TestScreen({super.key});
@override
State<TestScreen> createState() => _TestScreenState();
}
class _TestScreenState extends State<TestScreen> implements NetworkResponce {
//
@override
void onTaskComplete(String result, String caller) {
print("Result :: $result \nCaller :: $caller");
}
@override
void onTaskError(String error, String caller) {
print("Error :: $error \nCaller :: $caller");
}
getData() async {
NetworkUtils task = NetworkUtils(
"https://example.com/api", 'getData', this);
await task.get();
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("HTTP Agent Example"),
),
body: Column(
children: [
ElevatedButton(
onPressed: () {
getData();
},
child: const Text("Fetch Data"))
],
),
);
}
}
NetworkUtils Class #
The NetworkUtils class is used to perform HTTP requests. It requires a URL, a caller identifier, and a callback object that implements the NetworkResponce interface.
GET Request
NetworkUtils task = NetworkUtils("https://example.com/api", 'getData', this);
await task.get();
POST Request
NetworkUtils task = NetworkUtils("https://example.com/api", 'postData', this);
await task.post(data: {'key': 'value'});
PUT Request
NetworkUtils task = NetworkUtils("https://example.com/api/1", 'putData', this);
await task.put(data: {'key': 'newValue'});
DELETE Request
NetworkUtils task = NetworkUtils("https://example.com/api/1", 'deleteData', this);
await task.delete();
Multipart POST Request
NetworkUtils task = NetworkUtils("https://example.com/upload", 'uploadFile', this);
await task.multipartPost(
data: {'description': 'file upload'},
filePath: '/path/to/file.jpg'
);
NetworkResponce Interface #
The NetworkResponce interface defines two methods:
onTaskComplete(String result, String caller): Called when a request completes successfully.onTaskError(String error, String caller): Called when a request fails.
Implement this interface in your class to handle responses and errors.