【问题标题】:waiting to a Websocket response in flutter在颤动中等待 Websocket 响应
【发布时间】:2021-09-21 23:38:12
【问题描述】:

我正在尝试使用 web 套接字与颤振的服务器建立连接。有没有办法使用 Future 而不是 Streams 来收听响应。我需要的代码行是这样的:

 Future response = await ( expression to detect response from the server and return it);

这样的事情存在吗?

【问题讨论】:

    标签: flutter websocket


    【解决方案1】:
    void getFunction() async {
     var url = 'your URL';   
     late String result;
     final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      result = response.body;
    } else {
      print('Failed');
    }
    return result;
    

    你可以试试这个,

    【讨论】:

    • 有必要使用http.get()吗?我希望只使用 websockets 的东西
    • @Mufasa 您可以查看此链接。它使用websocket获取数据,如果有stackoverflow.com/a/55502749/7972633,你也可以捕获错误
    • 谢谢,很有帮助
    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2022-06-18
    • 1970-01-01
    • 2020-06-16
    • 2020-10-25
    相关资源
    最近更新 更多