【问题标题】:flutter connection with python颤振与python的连接
【发布时间】:2021-08-16 13:01:05
【问题描述】:

我一直在努力将 python 脚本与颤振连接起来,我的 python 脚本在浏览器中运行良好,但是在颤振中等待 http.get(url) 返回 null 或者你可以说它根本没有执行(因为我试图添加打印响应,但控制台中没有显示)。我正在使用本地服务器并使用与我的真实设备/电话连接的 USB 端口。
我正在使用的IP:`10.0.2.2:5000`,我也尝试过`127.0.0.1:5000`
FlatButton(
onPressed: () async {
     var response = await http.get(url);
     print('hi$response'); // from this line, nothing executing 
     if (response.statusCode == 200) {
          print(response.body);
          var decodedData = json.decode(response.body);
          setState(() {
              queryText = decodedData['Query'];
          });
          } else {
                print("server side error: ${response.body}");
          }
      },
child: Text("Click me")),

然后在一段时间后它会抛出connection time out 的异常。 更新: 我猜我的 await 有一些问题,因为它返回 null 并停止执行下面的代码行。
请帮忙。谢谢

【问题讨论】:

  • 您的笔记本电脑和手机是否在同一个 wifi 或任何其他网络上?除了USB
  • @KrishBhanushali 都在同一个 wifi 上。

标签: python flutter http


【解决方案1】:

我已经解决了这个问题,我猜await 没有返回任何东西,因此没有下面的行(甚至print 行)正在执行。我已在 app.run 中将 host 设置为 0.0.0.0 并在 Flutter 应用中使用 ipv4 地址,一切都开始工作了。
在 python 中
app.run(debug=True,host='0.0.0.0', port=5000)
在 Flutter 中(网址为):
http://192.168.1.2:5000/tt

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 2020-04-03
    • 2020-11-29
    • 2020-08-21
    • 2020-10-21
    • 2020-03-09
    • 2021-09-20
    • 2020-03-04
    • 1970-01-01
    相关资源
    最近更新 更多