duckma_client 0.0.4 copy "duckma_client: ^0.0.4" to clipboard
duckma_client: ^0.0.4 copied to clipboard

An interface with an adapter layer which tries to consume Dio with efficiency and smartly.

example/lib/main.dart

// Copyright (c) 2022 DuckMa Srl. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dio/dio.dart';
import 'package:duckma_client/duckma_client.dart';
import 'package:example/user/model/user/user_model.dart';
import 'package:example/user_detail.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: UsersScreen(),
    );
  }
}

class UsersScreen extends StatefulWidget {
  const UsersScreen({Key? key}) : super(key: key);

  @override
  State<UsersScreen> createState() => _UsersScreenState();
}

class _UsersScreenState extends State<UsersScreen> {
  late HttpClient _client;
  final List<UserModel?> _users = [];

  Future<void> getAllUsers() async {
    final Response<List> response = await _client.get(
      Uri(path: '/users'),
    );

    final List<UserModel> model =
        response.body.map((e) => UserModel.fromJson(e)).toList();
    _users.addAll(model);

    setState(() {});
  }

  void onNavToDetail(int index) {
    if (_users[index] != null) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) {
            return UserDetailScreen(userModel: _users[index]!);
          },
        ),
      );
    }
  }

  @override
  void initState() {
    super.initState();
    Dio dio = Dio();
    dio.options.baseUrl = 'https://jsonplaceholder.typicode.com';
    final connectivity = Connectivity();
    final requestRetrier = RequestRetrier(dio);
    final connectivityRequestRetrier = ConnectivityRequestRetrier(
      requestRetrier,
      connectivity,
    );
    _client = HttpClient(
      dio,
      connectivityRequestRetrier: connectivityRequestRetrier,
    );

    getAllUsers();
  }

  @override
  void dispose() {
    super.dispose();
    _client.close();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Users'),
      ),
      body: ListView.builder(
        itemCount: _users.length,
        itemBuilder: (BuildContext context, int index) {
          final UserModel? user = _users[index];
          if (user != null) {
            return ListTile(
              leading: Text(user.id.toString()),
              title: Text(user.name),
              subtitle: Text(user.email),
              onTap: () => onNavToDetail(index),
            );
          }
          return const Center(
            child: Text('Error to the user'),
          );
        },
      ),
    );
  }
}
0
likes
120
pub points
25%
popularity

Publisher

verified publisherduckma.com

An interface with an adapter layer which tries to consume Dio with efficiency and smartly.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

connectivity_plus, dio, meta

More

Packages that depend on duckma_client