api_exception 0.0.3 copy "api_exception: ^0.0.3" to clipboard
api_exception: ^0.0.3 copied to clipboard

Handle API Exception

example/lib/main.dart

import 'package:api_exception_example/cubit/home_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:api_exception/exception.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: HomePage(),
    );
  }
}

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

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => HomeCubit()..onInitData(),
      child: BlocConsumer<HomeCubit, HomeState>(
        listener: (context, state) {
          if (state.status.isFailure) {
            ScaffoldMessenger.of(context)
              ..hideCurrentSnackBar
              ..showSnackBar(
                SnackBar(
                  backgroundColor: Colors.red,
                  content: Text(
                    state.exception?.errorMessage(context) ?? "Unknown Error",
                    style: const TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              );
          }
        },
        builder: (context, state) {
          return Scaffold(
            appBar: AppBar(
              title: const Text("Example get data"),
            ),
            body: state.status.isLoading || state.status.isFailure
                ? const Center(
                    child: CircularProgressIndicator(),
                  )
                : const Text("Success Get Data"),
          );
        },
      ),
    );
  }
}
2
likes
130
points
41
downloads

Publisher

verified publisherjogjadev.my.id

Weekly Downloads

Handle API Exception

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

equatable, flutter, http

More

Packages that depend on api_exception