bloc_error_control 1.1.1 copy "bloc_error_control: ^1.1.1" to clipboard
bloc_error_control: ^1.1.1 copied to clipboard

Declarative error handling for BLoC with Zones and optional code generation

example/lib/main.dart

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

import 'bloc/user_bloc.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'BlocErrorControlMixin-Demo',
      theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple)),
      home: BlocProvider(lazy: false, create: (context) => UserBloc(), child: const MyHomePage()),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final buttonStyle = ButtonStyle(
      backgroundColor: WidgetStateProperty.all(Colors.blueAccent),
      foregroundColor: WidgetStateProperty.all(Colors.white),
    );
    return Scaffold(
      body: BlocBuilder<UserBloc, UserState>(
        builder: (context, state) {
          final child = switch (state) {
            UserLoaded() => Text(state.name),
            UserError() => Text(state.message),
            _ => const Text('loading...'),
          };

          return Center(child: child);
        },
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          TextButton(
            style: buttonStyle,
            onPressed: () {
              context.read<UserBloc>().add(const LoadUser1Event('User1'));
            },
            child: const Text('LoadUser1Event'),
          ),
          TextButton(
            style: buttonStyle,
            onPressed: () {
              context.read<UserBloc>().add(const LoadUser2Event('User2'));
            },
            child: const Text('LoadUser2Event'),
          ),
          TextButton(
            style: buttonStyle,
            onPressed: () {
              context.read<UserBloc>().add(const LoadUser3Event('User3'));
            },
            child: const Text('LoadUser3Event'),
          ),
        ],
      ),
    );
  }
}
1
likes
160
points
175
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Declarative error handling for BLoC with Zones and optional code generation

Repository (GitHub)
View/report issues

Topics

#bloc #error-handling #flutter #code-generation

License

MIT (license)

Dependencies

flutter, flutter_bloc, meta

More

Packages that depend on bloc_error_control