compute () en flutter n'a aucun effet
Sep 19 2020
J'essaye d'utiliser computedans Flutter. Ici, j'essaye de passer plusieurs paramètres dans une carte. Mais le code de ma fonction myFunctionne fonctionne pas. Je n'obtiens aucune erreur ou autre chose. Mon code semble ignoré. Trouvez-vous une erreur ici?
Fonction de calcul:
Map map = Map();
map['resultList'] = resultList;
map['_getImageFileFromAssets'] = _getImageFileFromAssets;
map["picturesData"] = picturesData;
map["albumID"] = albumID;
await compute(myFunction, map);
Appelle la fonction suivante:
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;
}
Réponses
kazume Sep 19 2020 at 11:30
Ok, un peu de code - je mets ceci dans 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;
}
et obtenez ceci comme résultat de console:
Starten
true
Aussi: y a-t-il une raison pour laquelle vous avez besoin du paramètre "map" dans votre fonction dynamic? Sinon, je le déclarerais comme type Map(comme je l'ai fait maintenant).