Skip to content

Instantly share code, notes, and snippets.

@felangel
Last active January 1, 2024 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felangel/92b256270c5567210285526a07b4cf21 to your computer and use it in GitHub Desktop.
Save felangel/92b256270c5567210285526a07b4cf21 to your computer and use it in GitHub Desktop.
Recipe: Bloc Access (Anonymous Routes)
import 'package:flutter/material.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class SimpleBlocDelegate extends BlocDelegate {
@override
void onEvent(Bloc bloc, Object event) {
super.onEvent(bloc, event);
print(event);
}
@override
void onTransition(Bloc bloc, Transition transition) {
super.onTransition(bloc, transition);
print(transition);
}
@override
void onError(Bloc bloc, Object error, StackTrace stacktrace) {
super.onError(bloc, error, stacktrace);
print(error);
}
}
void main() {
BlocSupervisor.delegate = SimpleBlocDelegate();
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: BlocProvider(
create: (BuildContext context) => CounterBloc(),
child: HomePage(),
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final counterBloc = BlocProvider.of<CounterBloc>(context);
return Scaffold(
appBar: AppBar(title: Text('Counter')),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute<CounterPage>(
builder: (context) {
return BlocProvider.value(
value: counterBloc,
child: CounterPage(),
);
},
),
);
},
child: Text('Counter'),
),
),
floatingActionButton: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: FloatingActionButton(
heroTag: 0,
child: Icon(Icons.add),
onPressed: () {
counterBloc.add(CounterEvent.increment);
},
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: FloatingActionButton(
heroTag: 1,
child: Icon(Icons.remove),
onPressed: () {
counterBloc.add(CounterEvent.decrement);
},
),
),
],
),
);
}
}
class CounterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Counter'),
),
body: BlocBuilder<CounterBloc, int>(
builder: (context, count) {
return Center(
child: Text('$count'),
);
},
),
);
}
}
enum CounterEvent { increment, decrement }
class CounterBloc extends Bloc<CounterEvent, int> {
@override
int get initialState => 0;
@override
Stream<int> mapEventToState(CounterEvent event) async* {
switch (event) {
case CounterEvent.decrement:
yield state - 1;
break;
case CounterEvent.increment:
yield state + 1;
break;
}
}
}
@SamsadSajid
Copy link

Where to dispose the counterBloc?

@felangel
Copy link
Author

@samadsajid it is automatically disposed by BlocProvider when you instantiate it inside the builder.

@ixsans
Copy link

ixsans commented Sep 1, 2020

I tried to put BlocProvider as argument of runApp method in main.dart like this:

void main() {
  BlocSupervisor.delegate = SimpleBlocDelegate();
  runApp(BlocProvider(
    create: (BuildContext context) => CounterBloc(),
    child: App(),
  ));
}

Then i can navigate from Home page to Counter page without Provider.value():

Navigator.of(context).push(
              MaterialPageRoute<CounterPage>(
                builder: (context) {
                  return CounterPage();
                },
              ),

And i still can access the bloc instance inside Counter page.
Just realise that this way i can access counter bloc instance across all page/routes in our page.

@felangel Is it recommended to creating all blocs of our app and provide it using BlocProvider/MultiBlocProvider by passing it in runApp() like above?

So, in my app, runApp method will looks like here:

runApp(
  MultiBlocProvider(
        providers: [
          BlocProvider(create: (_) => BlocA()),
          BlocProvider(create: (_) => BlocB()),
          BlocProvider(create: (_) => BlocC()),
          BlocProvider(create: (_) => BlocD()),
        ],
        child: HomePage()
      ),
);

@Iri-Hor
Copy link

Iri-Hor commented Jan 1, 2024

updated code to flutter_bloc 8.1.3

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

@immutable
class SimpleBlocObserver extends BlocObserver  {
  @override
  void onEvent(Bloc bloc, Object? event) {
    super.onEvent(bloc, event);
    // ignore: avoid_print
    print(event ?? 'event was null');
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    // ignore: avoid_print
    print(transition);
  }

  @override
  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
    super.onError(bloc, error, stackTrace);
    // ignore: avoid_print
    print(error);
  }
}

void main() {
  Bloc.observer = SimpleBlocObserver();
  runApp(const App());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: BlocProvider(
        create: (BuildContext context) => CounterBloc(),
        child: const HomePage(),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final counterBloc = BlocProvider.of<CounterBloc>(context);
    return Scaffold(
      appBar: AppBar(title: const Text('Counter')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.of(context).push(
              MaterialPageRoute<CounterPage>(
                builder: (context) {
                  return BlocProvider.value(
                    value: counterBloc,
                    child: const CounterPage(),
                  );
                },
              ),
            );
          },
          child: const Text('Counter'),
        ),
      ),
      floatingActionButton: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              heroTag: 0,
              child: const Icon(Icons.add),
              onPressed: () {
                counterBloc.add(Increment());
              },
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              heroTag: 1,
              child: const Icon(Icons.remove),
              onPressed: () {
                counterBloc.add(Decrement());
              },
            ),
          ),
        ],
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Counter'),
      ),
      body: BlocBuilder<CounterBloc, int>(
        builder: (context, count) {
          return Center(
            child: Text('$count'),
          );
        },
      ),
    );
  }
}

@immutable
sealed class CounterEvent {}
final class Increment extends CounterEvent {}
final class Decrement extends CounterEvent {}


class CounterBloc extends Bloc<CounterEvent, int> {
  CounterBloc() : super(0) {
    on<Increment>((event, emit) => emit(state+1));
    on<Decrement>((event, emit) => emit(state-1));
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment