Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

flutter - Send request over REST, receive result over Websocket, how to handle this with Future or other approach?

I'm quite a newbie when i comes to flutter/dart, but i kinda getting into it, it is just like java, only a bit shorter to write.

My flutter app sends a search query to my server which sends the query to many subsystems and finally the subsystem that handles the query sends the result back to the app. Very loosely coupled with message bus/queues and topics.

My plan was to create a future in my flutter app which widgets would call and "await" for the result. But because the search request goes to my server over REST and the result comes back over a STOMP websocket I'm having trouble figuring out how to give the future it's result (completed or error).

For example:

Future<SearchResult> search(String query) async {
    return server.sendRESTrequest(query);
    //how do i set complete or error state on the returned future?
}

Now a second or a few seconds later the result comes in over a websocket. I can't just block everything that comes over the websocket to wait for the correct response.

In my webgui i just send the request over REST and when a message comes in over the websocket i broadcast it with a eventbus. In the meantime i display a progress indicator until a result for my request is received through the eventbus.

Am i right that a Future isn't the right thing for this situation? I kinda liked it cause it does hide a lot of nasty things from my widgets: just get a future and await.

Is there maybe another (maybe better way) to approach this problem?

question from:https://stackoverflow.com/questions/65865790/send-request-over-rest-receive-result-over-websocket-how-to-handle-this-with-f

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...