smart_fetch 1.0.5 copy "smart_fetch: ^1.0.5" to clipboard
smart_fetch: ^1.0.5 copied to clipboard

Beautiful full-screen error pages for server side errors and no internet connection state.

example/smart_fetch_example.dart

import 'package:flutter/material.dart';
import 'package:smart_fetch/smart_fetch.dart';
import 'package:http/http.dart' as http;

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

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

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('SmartFetch Examples')),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          _buildExampleCard(
            context,
            title: 'Example 1: SmartFetch.builder()',
            description: 'Automatic error screens',
            child: SmartFetch.builder(
              future: () => http.get(Uri.parse('https://api.example.com/data')),
              onSuccess: (context, response) {
                return Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Icon(Icons.check_circle, color: Colors.green, size: 48),
                      const SizedBox(height: 16),
                      Text('Success: ${response.body}'),
                    ],
                  ),
                );
              },
              loadingWidget: const Center(child: CircularProgressIndicator()),
            ),
          ),
          const SizedBox(height: 16),
          _buildExampleCard(
            context,
            title: 'Example 2: SmartFetch.call() with Error Screens',
            description: 'Show error screens using context',
            child: Center(
              child: ElevatedButton(
                onPressed: () => _makeApiCall(context),
                child: const Text('Make API Call'),
              ),
            ),
          ),
          const SizedBox(height: 16),
          _buildExampleCard(
            context,
            title: 'Example 3: Direct Screen Access',
            description: 'Use error screens directly',
            child: Column(
              children: [
                ElevatedButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (_) => const NoInternetScreen(),
                      ),
                    );
                  },
                  child: const Text('Show No Internet Screen'),
                ),
                const SizedBox(height: 8),
                ElevatedButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (_) => const ServerErrorScreen(),
                      ),
                    );
                  },
                  child: const Text('Show Server Error Screen'),
                ),
                const SizedBox(height: 8),
                ElevatedButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (_) => const TimeoutScreen(),
                      ),
                    );
                  },
                  child: const Text('Show Timeout Screen'),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildExampleCard(
    BuildContext context, {
    required String title,
    required String description,
    required Widget child,
  }) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(title, style: Theme.of(context).textTheme.titleLarge),
            const SizedBox(height: 4),
            Text(description, style: Theme.of(context).textTheme.bodyMedium),
            const SizedBox(height: 16),
            SizedBox(height: 200, child: child),
          ],
        ),
      ),
    );
  }

  Future<void> _makeApiCall(BuildContext context) async {
    final result = await SmartFetch.call(
      () => http.get(Uri.parse('https://api.example.com/data')),
      timeout: const Duration(seconds: 10),
    );

    if (result.isSuccess) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('API call successful!')),
      );
    } else {
      // Use helper method to show error screen
      SmartFetchNavigator.showErrorScreen(context, result);
    }
  }
}
3
likes
0
points
481
downloads

Publisher

unverified uploader

Weekly Downloads

Beautiful full-screen error pages for server side errors and no internet connection state.

Repository (GitHub)
View/report issues

Topics

#flutter #error-handling #timeout #no-internet #server-error

License

unknown (license)

Dependencies

connectivity_plus, flutter, http, lottie

More

Packages that depend on smart_fetch