flutter中的compute()没有效果

2024-04-25

我尝试使用compute在颤振中。这里我尝试在 Map 内传递多个参数。但是我的函数中的代码myFunction不起作用。我没有收到任何错误或其他信息。我的代码似乎被忽略了。你发现这里有错误吗?

计算函数:

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;
}

好的,一些代码 - 我把它放进去dartpad.dev https://dartpad.dev/536540b5075382941b7f4b823b114181:

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;
}

并将其作为控制台结果:

Starten
true

另外:是否有理由需要函数中的“map”参数dynamic?如果没有,我会将其声明为类型Map(就像我现在做的那样)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

flutter中的compute()没有效果 的相关文章

随机推荐