本文介绍了扑中的compute()无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Flutter中使用 compute 。在这里,我尝试在地图内传递多个参数。但是我的函数 myFunction 中的代码不起作用。我没有错误或其他问题。我的代码似乎被忽略了。您在这里找到错误吗?

I try to use compute in Flutter. Here I try to pass multiple parameters inside a Map. But the code in my function myFunction does not work. I get no errors or something else. My code seems to be ignored. Do you find an error here?

计算功能:

Map map = Map();
map['resultList'] = resultList;
map['_getImageFileFromAssets'] = _getImageFileFromAssets;
map["picturesData"] = picturesData;
map["albumID"] = albumID;

await compute(myFunction, map);

调用以下函数:

Future<bool> myFunction(map) async {
  var resultList = map["resultList"];
  var _getImageFileFromAssets = map["_getImageFileFromAssets"];
  var picturesData = map["picturesData"];
  var albumID = map["albumID"];

  print("Starten");

  for (var i = 0; i < resultList.length; i++) {
    print(i);
    File imageFile = await _getImageFileFromAssets(resultList[i]);

    final appDir = await syspath.getApplicationDocumentsDirectory();

    final fileName = path.basename(imageFile.path);

    final savedImage =
        await File(imageFile.path).copy('${appDir.path}/$fileName');

    // Creating thumbnails

    final thumb = image.decodeImage(await File(savedImage.path).readAsBytes());
    final thumbImage = image.copyResize(thumb, width: 500);

    new File('${appDir.path}/$fileName-thumb-500.jpg')
        .writeAsBytes(image.encodeJpg(thumbImage));

    final finalThumbImage = File('${appDir.path}/$fileName-thumb-500.jpg');

    picturesData.add(Picture(
        album: albumID,
        path: savedImage.path,
        thumbPath: finalThumbImage.path,
        timestamp: Timestamp.now()));
  }

  return true;
}


推荐答案

好的,有些代码-我将其放在:

Ok, some code - I put this in dartpad.dev:

import 'package:flutter/foundation.dart';

void main() {
  Map map = Map();
  compute(myFunction, map).then((result) => print(result));
}

Future<bool> myFunction(Map map) async {
  print("Starten");
  // fake long process
  await Future.delayed(Duration(seconds: 5));
  return true;
}

并将其作为控制台结果:

and get this as a console result:

Starten
true

此外:是否有您需要地图您的函数中的参数是动态?如果没有,我将其声明为 Map 类型(就像我现在所做的那样)。

Also: is there a reason you need the "map" parameter in your function to be dynamic? If not, I'd declare it as type Map (like I did now).

这篇关于扑中的compute()无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 21:32