Skip to content

Example using Dart isolates to run run compute tasks in Flutter

Notifications You must be signed in to change notification settings

jeffhigham-f3/flutter_isolates

Repository files navigation

Flutter Isolates

Example counter app using isolates to process data. For demonstration only, generally you should never need to spawn a dedicated Isolate.

import 'dart:isolate';
void _uselessCounter(int start) {
  var count = start;
  while (true) {
    sleep(
      const Duration(
        seconds: 5,
      ),
    );
    debugPrint('isolate: $count');
    count++;
  }
}
  @override
  void initState() {
    super.initState();
    Isolate.spawn(_uselessCounter, 0);
  }
class CounterCubit extends Cubit<int> {
  CounterCubit() : super(0);

  void increment() async => emit(
        await compute(
          (int c) {
            sleep(const Duration(seconds: 1));
            return c + 1;
          },
          state,
        ),
      );

  void decrement() async => emit(
        await compute((int c) => c - 1, state),
      );
}

About

Example using Dart isolates to run run compute tasks in Flutter

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published