isolate_bloc 0.1.2 copy "isolate_bloc: ^0.1.2" to clipboard
isolate_bloc: ^0.1.2 copied to clipboard

outdated

A dart package that helps implement the BLoC pattern. BLoC works in Isolate and don't slow down UI.

example/lib/main.dart

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

void main(List<String> arguments) async {
  await initialize(isolatedFunc);
  runApp(
    MaterialApp(
      home: IsolateBlocProvider<CounterBloc, int>(
        child: CounterScreen(),
      ),
    ),
  );
}

class CounterScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Counter'),
      ),
      body: Center(
        child: IsolateBlocListener<CounterBloc, int>(
          listener: (context, state) => print("New bloc state: $state"),
          child: IsolateBlocBuilder<CounterBloc, int>(
            builder: (context, state) {
              return Text('You tapped $state times');
            },
          ),
        ),
      ),
      floatingActionButton: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          FloatingActionButton(
            heroTag: 'Increment',
            onPressed: () => context
                .isolateBloc<CounterBloc, int>()
                .add(CountEvent.increment),
            child: Icon(Icons.add),
          ),
          SizedBox(height: 10),
          FloatingActionButton(
            heroTag: 'Decrement',
            onPressed: () => context
                .isolateBloc<CounterBloc, int>()
                .add(CountEvent.decrement),
            child: Icon(Icons.remove),
          ),
        ],
      ),
    );
  }
}

Future<void> isolatedFunc() async {
  IsolateBloc.observer = SimpleBlocObserver();
  register(create: () => CounterBloc());
}

class CounterBloc extends IsolateBloc<CountEvent, int> {
  CounterBloc() : super(0);

  @override
  void onEventReceived(CountEvent event) {
    emit(event == CountEvent.increment ? state + 1 : state - 1);
  }
}

enum CountEvent {
  increment,
  decrement,
}

class SimpleBlocObserver extends IsolateBlocObserver {
  void onEvent(IsolateBloc bloc, Object event) {
    print("New $event for $bloc");
    super.onEvent(bloc, event);
  }

  void onTransition(IsolateBloc bloc, Transition transition) {
    print("New ${transition.nextState} from $bloc");
    super.onTransition(bloc, transition);
  }

  void onError(IsolateBloc bloc, Object error, StackTrace stackTrace) {
    print("$error in $bloc");
    super.onError(bloc, error, stackTrace);
  }
}
44
likes
0
pub points
67%
popularity

Publisher

unverified uploader

A dart package that helps implement the BLoC pattern. BLoC works in Isolate and don't slow down UI.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, nested, provider, uuid

More

Packages that depend on isolate_bloc