flutterx_http 1.0.1-dev
flutterx_http: ^1.0.1-dev copied to clipboard
A useful wrapper library of dart.dev http library for HTTP requests.
flutterx_http #
A useful wrapper library of dart.dev http library for HTTP requests.
Install #
Add a line like this to your package's pubspec.yaml (and run dart pub get):
dependencies:
flutterx_http: ^1.0.1-dev
Import #
Import the library like this:
import 'package:flutterx_http/flutterx_http.dart';
Usage #
The most easy example of usage of this library is:
import 'package:flutter/material.dart';
import 'package:flutterx_http/flutterx_http.dart';
import 'package:flutterx_utils/flutterx_utils.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutterx HTTP Demo',
theme: ThemeData(primarySwatch: Colors.lightGreen),
home: const HTTPExample());
}
class HTTPExample extends StatefulWidget {
const HTTPExample({Key? key}) : super(key: key);
@override
State<HTTPExample> createState() => _HTTPExampleState();
}
class _HTTPExampleState extends State<HTTPExample> {
final MyApi api = MyApi('https://my.domain.com');
bool _calling = false;
Result<MyData>? _data;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('HTTP example')),
body: Center(child: Text(_calling ? 'Performing call...' : _data.toString())),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() => _calling = true);
Result.wrapAsync(() => api.getMyData('ugo')).then((value) => setState(() {
_data = value;
_calling = false;
}));
},
tooltip: 'Send',
child: const Icon(Icons.add)));
}
class MyApi {
final XHttpClient _client;
MyApi(String baseUrl) : _client = XHttpClient(baseUrl: baseUrl);
static final ApiCall<MyData> _getMyData = ApiCall.object<MyData>(
request: (client, params) => client.get('/api/myData', params: params), fromJson: MyData.fromJson)
.mock(response: (params) => const MyData(name: 'Ugo', age: 37)); // remove this line if you have a real endpoint
Future<MyData> getMyData(String userId) => _getMyData(_client, {'userId': userId});
}
class MyData with DTO {
final String name;
final int age;
const MyData({required this.name, required this.age});
static MyData fromJson(JsonObject json) => MyData(name: json['name'], age: json['age']);
@override
JsonObject toJson() => {'name': name, 'age': age};
}
But it can do much more, please check the documentation in the desired source file of this library