Skip to content
This repository has been archived by the owner on May 31, 2023. It is now read-only.

Your code should not run on the main thread #70

Open
liuzho opened this issue Apr 3, 2020 · 4 comments
Open

Your code should not run on the main thread #70

liuzho opened this issue Apr 3, 2020 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@liuzho
Copy link

liuzho commented Apr 3, 2020

Your code should not run on the main thread. This will cause the program to freeze. All IO operations / time-consuming operations should be on the worker thread.

@gwsh
Copy link

gwsh commented Sep 14, 2020

Your code should not run on the main thread. This will cause the program to freeze. All IO operations / time-consuming operations should be on the worker thread.

Efficiency first.

@JeanChristopheMobizel
Copy link

Same issue here, I need to display a loader, but UI is stuck by flutter_native_image

@btastic btastic added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Apr 7, 2021
@giaur500
Copy link

You can try to use compute so it will be executed in isolate

@JamesMcIntosh
Copy link

@giaur500 compute does not have access to Flutter plugins so you will have to use an alternative library such as isolate_handler to construct the isolate.
https://pub.dev/packages/isolate_handler

import 'dart:io';
import 'package:flutter_native_image/flutter_native_image.dart';
import 'package:isolate_handler/isolate_handler.dart';

typedef OnFinishedResizing = void Function(bool success);

class ImageResize {

  static final IsolateHandler isolates = IsolateHandler();
  static final String ImageResizeIsolateName = "image-resize";

  static void process(Map<String, String> files, OnFinishedResizing onFinished) {
    isolates.spawn<bool>(
      _onResize,
      name: ImageResizeIsolateName,
      onReceive: (bool success) {
        _finish();

        onFinished(success);
      },
      onInitialized: () => isolates.send(files, to: ImageResizeIsolateName),
    );
  }

  static void _finish() {
    isolates.kill(ImageResizeIsolateName);
  }

  static void _onResize(Map<String, dynamic> context) {
    final HandledIsolateMessenger messenger = HandledIsolate.initialize(context);

    messenger.listen((dynamic files) async {
      if (files is Map<String, String>) {
        if (files.entries.isNotEmpty) {
          await Future.wait(files.entries.map((entry) => _resize(File(entry.key), File(entry.value))));

          messenger.send(true);

          return;
        }
      }

      messenger.send(false);
    });
  }

  static Future<File> _resize(File source, File output) async {
    final int width = 100;
    final int height = 100;
    final File compressed = await FlutterNativeImage.compressImage(
      source.path,
      quality: 90,
      targetWidth: width,
      targetHeight: height,
    );

    output.writeAsBytesSync(compressed.readAsBytesSync(), flush: true);

    return output;
  }

}

To use

final Map<String, String> images = {
  "path to image source 1": "path to image destination 1",
  "path to image source 2": "path to image destination 2",
};

ImageResize.process(images, (bool success) {
  // do something with the resulting images
  images.values.map(print);
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants