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

outdated

A new Flutter package project.

A dart package that helps implement the BLoC pattern.

Attention #

This package now in alpha and you should not use it in any not pet project. If you find a bug or want some new feature please create a new issue.

Overview #

You can read about BLoC pattern here.

The main difference from another BLoC pattern implementations is what blocs works in Isolate and don't slow down UI.

Creating a Bloc #

class CounterBloc extends IsolateBloc<CountEvent, int> {
  /// The initial state of the `CounterBloc` is 0.
  CounterBloc() : super(0);

  /// When `CountEvent` is received, the current state
  /// of the bloc is accessed via `state` and
  /// a new `state` is emitted via `emit`.
  @override
  void onEventReceived(CountEvent event) {
    emit(event == CountEvent.increment ? ++state : --state);
  }
}

Registering a Bloc #

void main() async {
  await initialize(isolatedFunc);
  ...
}

/// Global function which is used to register blocs and called in Isolate
void isolatedFunc() {
  /// Register a bloc to be able to create it in main Isolate
  register(create: () => CounterBloc());
}

Using Bloc in UI #

YourWidget(
  /// Create CounterBloc and provide it down to the widget tree
  child: IsolateBlocProvider<CounterBloc, int>(
    child: CounterScreen(),
  ),
)
...
class CounterScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Counter'),
      ),
      body: Center(
        /// Listen for CounterBloc State
        child: IsolateBlocListener<CounterBloc, int>(
          listener: (context, state) => print("New bloc state: $state"),
          /// Build widget based on CounterBloc's State
          child: IsolateBlocBuilder<CounterBloc, int>(
            builder: (context, state) {
              return Text('You tapped $state times');
            },
          ),
        ),
      ),
      floatingActionButton: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          FloatingActionButton(
            heroTag: 'Increment',
            /// Get bloc using extension and add new state
            onPressed: () => context.isolateBloc<CounterBloc>().add(CountEvent.increment),
            child: Icon(Icons.add),
          ),
          SizedBox(height: 10),
          FloatingActionButton(
            heroTag: 'Decrement',
            /// Get bloc using provider class and add new state
            onPressed: () => IsolateBlocProvider.of<CounterBloc>(context).add(CountEvent.decrement),
            child: Icon(Icons.remove),
          ),
        ],
      ),
    );
  }
}

All Api #

Initialization #

Initialize all services required to work with IsolateBloc and register an IsolateBloc. isolatedFunc may be a future and MUST be a GLOBAL or STATIC function.

void main() async {
  /// Initialize
  await initialize(isolatedFunc);
  ...
}

/// Global function which is used to register blocs and called in Isolate
void isolatedFunc() {
  /// Register a bloc to be able to create it in main Isolate
  register(create: () => CounterBloc());
}

Create new Bloc instance #

To create new instance of bloc you can use Widget or function

/// Create with Widget
IsolateBlocProvider<BlocA, BlocAState>(
    child: ChildA(),
)
/// Create with function
final blocA = createBloc<BlocA, BlocAState>();

Get a Bloc #

IsolateBlocBuilder<CounterBloc, int>(
  buildWhen: (state, newState) {
    /// return true/false to determine whether or not
    /// to rebuild the widget with state
  builder: (context, state) {
    // return widget here based on BlocA's state
  },
)

IsolateBlocListener<CounterBloc, int>(
  listenWhen: (state, newState) {
    /// return true/false to determine whether or not
    /// to listen for state
  },
  listener: (context, state) {
    /// listen for state
  },
  child: ChildWidget(),
)

Create Bloc Observer #

void main() {
  IsolateBloc.observer = SimpleBlocObserver();
  ...
}

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

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

  void onError(IsolateBlocWrapper bloc, Object error, [StackTrace stackTrace]) {
    print("Error $error in $bloc");
    super.onError(bloc, error, stackTrace);
  }
}

Examples #

44
likes
0
pub points
67%
popularity

Publisher

unverified uploader

A new Flutter package project.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, nested, provider, uuid

More

Packages that depend on isolate_bloc