0

My main works, but in my foo I would like to send data to main. Here's my code where I want to pass data between foo and main 100 times. How can I achieve that?

import 'dart:isolate';  
import 'dart:async';
void foo(SendPort sendPort) async {
  ReceivePort receivePort = new ReceivePort();
  sendPort.send(receivePort.sendPort);
      receivePort.listen((dataSend){
      print('foo  received : ${dataSend}');
      print('');
    });
}
void main() async {
      int temp = 0;
      ReceivePort receivePort = new ReceivePort(); 
      Isolate.spawn(foo,receivePort.sendPort);  
      receivePort.listen((dataSend) {   
      print('I received : ${dataSend}');      
      dataSend.send(temp+1);  
      });   
}

anticafe
  • 6,816
  • 9
  • 43
  • 74

1 Answers1

0

I am really confused about what you want to achieve but is it something like this?

import 'dart:isolate';
import 'dart:async';

void foo(SendPort sendPort) async {
  ReceivePort receivePort = new ReceivePort();
  sendPort.send(receivePort.sendPort);
  receivePort.listen((dataSend) {
    print('foo  received : ${dataSend}');
    sendPort.send(receivePort.sendPort);
  });
}

void main() async {
  int temp = 0;
  ReceivePort receivePort = new ReceivePort();
  Isolate.spawn(foo, receivePort.sendPort);
  receivePort.listen((dataSend) {
    print('I received : ${dataSend}');
    if (++temp < 100) {
      dataSend.send(temp);
    } else {
      receivePort.close();
    }
  });
}
julemand101
  • 28,470
  • 5
  • 52
  • 48
  • yes is almost, but like example when Isolate.spawn(foo, receivePort.sendPort); it will send sendPort to foo() right? but i want to send also the temp = 0 or 0 value and then when foo() receive the data, foo() can get the 0 value and inside the foo() will add + 2 or like 0 + 2 and the answer for that will send it to main() thats why i have receiveport() and then the main receive 2 right and then the main will +1 or like 1+ 2 and then send again to foo() vice versa 100 times.. i hope you understand my explanation.. – prriiinnnn May 04 '19 at 06:05
  • You are allowed to send a List as input if you need to transfer multiple data. – julemand101 May 05 '19 at 13:08