Last updated:
0 purchases
httpagent
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
copied to clipboard
Then run:
flutter pub get
copied to clipboard
Usage #
Import the package in your Dart file:
import 'package:httpagent/httpagent.dart';
copied to clipboard
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"))
],
),
);
}
}
copied to clipboard
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();
copied to clipboard
POST Request
NetworkUtils task = NetworkUtils("https://example.com/api", 'postData', this);
await task.post(data: {'key': 'value'});
copied to clipboard
PUT Request
NetworkUtils task = NetworkUtils("https://example.com/api/1", 'putData', this);
await task.put(data: {'key': 'newValue'});
copied to clipboard
DELETE Request
NetworkUtils task = NetworkUtils("https://example.com/api/1", 'deleteData', this);
await task.delete();
copied to clipboard
Multipart POST Request
NetworkUtils task = NetworkUtils("https://example.com/upload", 'uploadFile', this);
await task.multipartPost(
data: {'description': 'file upload'},
filePath: '/path/to/file.jpg'
);
copied to clipboard
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.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.