rest_ez 0.1.0+2 copy "rest_ez: ^0.1.0+2" to clipboard
rest_ez: ^0.1.0+2 copied to clipboard

A comprehensive and easy-to-use REST API client package for Flutter applications with built-in error handling, timeout management, and web file download support.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:rest_ez/rest_ez.dart';

void main() {
  // Initialize RestEz client
  RestEzClient.instance.initialize(
    config: const RestEzConfig(
      baseUrl: 'https://jsonplaceholder.typicode.com',
      defaultTimeout: Duration(seconds: 30),
      enableLogging: true,
    ),
  );

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'RestEz Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const ExampleScreen(),
    );
  }
}

class ExampleScreen extends StatefulWidget {
  const ExampleScreen({super.key});

  @override
  State<ExampleScreen> createState() => _ExampleScreenState();
}

class _ExampleScreenState extends State<ExampleScreen> {
  User? user;
  bool isLoading = false;
  String? error;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('RestEz Example'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            ElevatedButton(
              onPressed: isLoading ? null : _fetchUser,
              child: isLoading
                  ? const CircularProgressIndicator()
                  : const Text('Fetch User'),
            ),
            const SizedBox(height: 20),
            if (error != null)
              Container(
                padding: const EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Colors.red.shade100,
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Text(
                  'Error: $error',
                  style: const TextStyle(color: Colors.red),
                ),
              ),
            if (user != null)
              Container(
                padding: const EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Colors.green.shade100,
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('ID: ${user!.id}'),
                    Text('Name: ${user!.name}'),
                    Text('Email: ${user!.email}'),
                    Text('Website: ${user!.website}'),
                  ],
                ),
              ),
          ],
        ),
      ),
    );
  }

  Future<void> _fetchUser() async {
    setState(() {
      isLoading = true;
      error = null;
      user = null;
    });

    try {
      final response = await RestEzClient.instance.get<User>(
        endpoint: '/users/1',
        fromJson: (json) => User.fromJson(json),
        checkToken: false, // No authentication required for this demo API
      );

      if (response.success && response.data != null) {
        setState(() {
          user = response.data;
        });
      } else {
        setState(() {
          error = response.error ?? 'Unknown error occurred';
        });
      }
    } catch (e) {
      setState(() {
        error = e.toString();
      });
    } finally {
      setState(() {
        isLoading = false;
      });
    }
  }
}

// Example data model
class User {
  final int id;
  final String name;
  final String email;
  final String website;

  User({
    required this.id,
    required this.name,
    required this.email,
    required this.website,
  });

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['id'] ?? 0,
      name: json['name'] ?? '',
      email: json['email'] ?? '',
      website: json['website'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'email': email,
      'website': website,
    };
  }
}
2
likes
140
points
0
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A comprehensive and easy-to-use REST API client package for Flutter applications with built-in error handling, timeout management, and web file download support.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, http

More

Packages that depend on rest_ez