channel sent a message from native to Flutter on a non-platform thread

Hello, in my code I implement firebase, but I always have this error, [ERROR:flutter/shell/common/shell.cc(1004)] The ‘plugins.flutter.io/firebase_storage/taskEvent/09f998da-a9d7-46a8-b865- 119987c06342’ channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel. this is my code…

import ‘dart:io’;
import ‘dart:typed_data’;
import ‘dart:isolate’;
import ‘package:flutter/foundation.dart’;
import ‘package:flutter/material.dart’;
import ‘package:path/path.dart’ as path;
import ‘package:image/image.dart’ as img;
import ‘package:firebase_storage/firebase_storage.dart’ as firebase_storage;
import ‘package:path_provider/path_provider.dart’;
import ‘package:flutter/services.dart’;

class ImageUploadService {
static Future uploadFile(
File file,
String folder,
String name,
int targetWidth,
) async {
final firebase_storage.Reference storageReference =
firebase_storage.FirebaseStorage.instance.ref(folder).child(name);

File? tempFile;
try {
if (targetWidth > 0) {
// Procesar la imagen en un isolate
tempFile = await Isolate.run(() => _processImage(file, targetWidth));

  if (tempFile == null) {
    debugPrint('Failed to process image');
    return '';
  }
}

// Volver al hilo principal para Firebase
final File fileToUpload = tempFile ?? file;

final firebase_storage.UploadTask uploadTask = storageReference.putFile(fileToUpload);
await uploadTask.whenComplete(() => null);

// Obtener la URL
final String url = await storageReference.getDownloadURL();
return '${url.split('?alt=media&token=')[0]}?alt=media';

} catch (e) {
debugPrint(‘Error en uploadFile: $e’);
return ‘’;
} finally {
// Limpiar archivo temporal si existe
if (tempFile != null && await tempFile.exists()) {
try {
await tempFile.delete();
} catch (e) {
debugPrint(‘Error al eliminar archivo temporal: $e’);
}
}
}
}

static File? _processImage(File file, int targetWidth) {
try {
final Uint8List imageBytes = file.readAsBytesSync();
final img.Image? originalImage = img.decodeImage(imageBytes);

if (originalImage == null) {
  debugPrint('Could not decode image');
  return null;
}

// Recortar imagen en cuadrado
final int size = originalImage.width < originalImage.height 
    ? originalImage.width 
    : originalImage.height;

final img.Image croppedImage = img.copyCrop(
  originalImage,
  x: 0,
  y: 0,
  width: size,
  height: size,
);

// Redimensionar si es necesario
final img.Image processedImage = targetWidth < size
    ? img.copyResize(
        croppedImage,
        width: targetWidth,
        height: targetWidth,
        interpolation: img.Interpolation.linear,
      )
    : croppedImage;

// Crear archivo temporal
final Directory tempDir = Directory.systemTemp;
final String tempFileName = 'temp_${DateTime.now().millisecondsSinceEpoch}.jpg';
final String tempPath = path.join(tempDir.path, tempFileName);
final File tempFile = File(tempPath);

// Guardar imagen procesada
tempFile.writeAsBytesSync(
  Uint8List.fromList(img.encodeJpg(processedImage, quality: 85))
);

return tempFile;

} catch (e) {
debugPrint(‘Error processing image: $e’);
return null;
}
}

}