How to return data from a thread to the layout at runtime on flutter

0

Is there a way to make a thread using the flutter and return the result of the processing on the screen?

I have the following code. In the main class _MyHomePageState I have the layout and function _processo () . The idea is to start the _process () function, create the IsolateRunner , Runner will call the _processar which will do a procedure and return, meanwhile the user can access the app and call other threads.

My problem is in the return, because I can not display this return on the screen, for example, I tried to return in the status variable and directly in the _process () function. From what I understood the return could only occur once, taking into account that only asynchronous functions are accepted.

Future<String> _processar(String foto) async {
   for (var i = 0; i < 10; i++) {
     sleep(const Duration(seconds:5)); 
     print('Thread - '+i.toString());
   }
   return 'Ok';
}

class _MyHomePageState extends State<MyHomePage> {
  Future<String> status;
  final String ass = 'asd';

 ..<código>..
   void _processo() async {
      final runner = await IsolateRunner.spawn();
      status = runner
           .run(_processar, 'foto')
           .whenComplete(() => (){
               runner.close();
               setState(() {
                   this.ass = status;
               });
      return this.status;
    });
   }
 ..<código>..
}
    
asked by anonymous 19.12.2018 / 13:42

1 answer

0

This code can do what you want, it creates an Isolate and returns a response each time it sends a "photo"

static _processar(SendPort sendPort) async {
  // Cria a porta para receber a foto
  ReceivePort port = ReceivePort();

  // Envia a mensagem inicial para a main thread a porta criada
  sendPort.send(port.sendPort);

  // Espera receber a foto
  await for (var msg in port) {
    // Recebe a foto
    String foto = msg[0];
    // Local para retornar a resposta
    SendPort replyTo = msg[1];

    for (var i = 0; i < 10; i++) {
      sleep(const Duration(seconds:5));
      print('Thread - '+i.toString());
    }
    replyTo.send("ok");
  }
}

class _MyHomePageState extends State<MyHomePage> {
  Future<String> status;
  final String ass = 'asd';

  // Código

  void _processo() async {
    //Cria a porta para receber a mensagem inicial do isolate
    ReceivePort receivePort = ReceivePort();
    // Cria o isolate e envia a porta de recebimento
    await Isolate.spawn(_processar, receivePort.sendPort);

    // Guarda a porta do isolate para enviar a foto (Recebimento da mensagem inicial)
    SendPort sendPort = await receivePort.first;

    // Envia a foto
    String msg = await sendReceive(sendPort, "foto");

    setState(() {
      status = msg;
    });
  }
}

// Função para enviar a foto para o isolate junto com a porta da resposta
Future sendReceive(SendPort port, msg) {
  ReceivePort response = ReceivePort();
  port.send([msg, response.sendPort]);
  return response.first;
}

I hope I have helped!

If you have any questions or want a more detailed explanation, this article explains in more detail: link

    
28.12.2018 / 17:34