parallel_dart 0.0.1+1 copy "parallel_dart: ^0.0.1+1" to clipboard
parallel_dart: ^0.0.1+1 copied to clipboard

Execute code in multicore easily.

example/lib/main.dart

import 'dart:io';

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

void main() {
  Parallel.initialize(
    numberOfIsolates: Platform.numberOfProcessors - 1,
    maxConcurrentPerIsolate: 100,
    onInitialization: () {},
  );

  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Parallel Dart',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Parallel Dart'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool isComputing = false;
  int number = 0;

  void simulateHeavyComputing(int num) {
    for (var i = 0; i < num; i++) {
      // Literally do nothing here
    }

    print('Heavy Computing Is Done');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Counter : ${number}'),
            SizedBox(height: 12),
            ElevatedButton(
              onPressed: isComputing
                  ? null
                  : () {
                      setState(() {
                        isComputing = true;
                      });
                      simulateHeavyComputing(1000000000000);
                    },
              child: Text('Run heavy process without parallel - It Will Free'),
            ),
            SizedBox(height: 12),
            ElevatedButton(
              onPressed: isComputing
                  ? null
                  : () {
                      setState(() {
                        isComputing = true;
                      });
                      Parallel.run(() async {
                        simulateHeavyComputing(1000000000000);
                      });
                    },
              child: Text('Run heavy process with parallel - Not Freeze'),
            ),
            SizedBox(height: 12),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            number++;
          });
        },
        child: Icon(Icons.add, color: Colors.white),
      ),
    );
  }
}