flutterx_http 1.0.0-dev copy "flutterx_http: ^1.0.0-dev" to clipboard
flutterx_http: ^1.0.0-dev copied to clipboard

outdated

A useful wrapper library of dart.dev http library for HTTP requests.

example/lib/main.dart

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};
}
1
likes
0
points
33
downloads

Publisher

unverified uploader

Weekly Downloads

A useful wrapper library of dart.dev http library for HTTP requests.

Repository (GitLab)
View/report issues

License

unknown (license)

Dependencies

flutter, flutterx_utils, http

More

Packages that depend on flutterx_http