How to return data from a thread to the runtime layout in flutter

Asked

Viewed 483 times

0

There is how to thread using the flutter and return the processing result on the screen?

I have the following code. In the main class _Myhomepagestate I have the layout and function _process(). The idea is to start the function _process(), create the Isolaterunner, the Runner will call the function _sue, that will do a procedure and return, while the user can access the app and call other threads.

My problem is in the return, because I can’t display this return on the screen, for example, I’ve tried to return in the variable status and directly in function _process(). 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>..
}
  • 1

    Why is it being done on a flat? Any particular reason?

1 answer

0

This code can do what you want, it creates a Isolate and returns a reply each time you send 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’ve helped!

If you have any questions or want a more detailed explanation have this article explaining in more detail: https://magrizo.wordpress.com/2018/12/28/como-usar-threads-no-flutter-com-a-biblioteca-isolate/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.