// ignore_for_file: unused_local_variable
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'dart:isolate';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:test/data/models/post_model.dart';
import '../../../functions/add_notification.dart';
Future<void> uploadInBackground(
List<File> data, PostModel posts) async {
try {
final ReceivePort receivePort = ReceivePort();
await Isolate.spawn(_uploadIsolate, receivePort.sendPort);
// Create a completer to listen for the isolate response
final Completer<void> completer = Completer<void>();
receivePort.listen((dynamic message) {
if (message is int) {
print('undefine');
// Update the progress in the notification
updateNotificationProgress(message);
}
else {
print('define');
// Upload complete or failed, close the notification
completer.complete();
flutterLocalNotificationsPlugin.cancel(0);
receivePort.close();
}
});
final send = receivePort;
// Send the data to the isolate
send.sendPort.send({'images': data, 'posts': posts});
return completer.future;
} catch (e) {
log('error in upload in backgroud');
print(e);
}
}
// Isolate function
Future<void> _uploadIsolate(SendPort sendPort) async {
print('running FIle');
try {
final ReceivePort receivePort = ReceivePort();
sendPort.send(receivePort.sendPort);
await for (dynamic message in receivePort) {
print('Received message in isolate: $message');
final List<File> files = message['images'];
final PostModel posts = message['posts'];
print(files);
print(posts);
final SendPort mainThreadSendPort = message['sendPort'];
try {
// Upload images to Firebase Storage
final List<String> imageUrls =
await _uploadImagesToStorage(files, mainThreadSendPort);
// Store image URLs in Cloud Firestore
await _storeImageUrlsInFirestore(imageUrls, posts);
mainThreadSendPort.send('Upload complete');
} catch (ex) {
mainThreadSendPort.send('Upload failed');
}
sendPort.send('Upload complete');
}
;
// );
} catch (ex) {
log('error in _upload Isolate');
print(ex);
}
}
// uploading function of images
Future<List<String>> _uploadImagesToStorage(
List<File> imageFiles, SendPort sendPort) async {
final List<String> imageUrls = [];
for (int i = 0; i < imageFiles.length; i++) {
final File imageFile = imageFiles[i];
final Reference storageReference = FirebaseStorage.instance
.ref('images')
.child(
'images/${DateTime.now().toIso8601String()}_$i.${imageFiles[i].path.split('.').last}');
final UploadTask uploadTask = storageReference.putFile(imageFile);
uploadTask.snapshotEvents.listen((TaskSnapshot snapshot) {
final double progress =
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
sendPort.send(progress.toInt());
});
await uploadTask;
// Add the download URL to the list
final String imageUrl = await storageReference.getDownloadURL();
imageUrls.add(imageUrl);
}
// Return the list of download URLs
return imageUrls;
}
// store images in database
Future<void> _storeImageUrlsInFirestore(
List<String> imageUrls, PostModel data) async {
data.images?.clear();
data.images?.addAll(imageUrls);
final imagesCollection = FirebaseFirestore.instance
.collection('User Posts')
.add(data.toFirestore());
// Store image URLs in Firestore
}
this is my upload file in this file i have added ome print statements to check where is the problem occurring. and it gives me prints in console they are I/flutter ( 2883): uploading I/flutter ( 2883): running FIle I/flutter ( 2883): define according to this in the fuction _uploadIsolate the listening to port is not working . does any one know hat is it happening . i already tried the receivePort.listen property but it is also giving the same . this is a program for uploading the data to firebase.