gpt4 book ai didi

dart - Flutter gRPC 错误 - 操作系统错误 : Connection refused

转载 作者:IT王子 更新时间:2023-10-29 06:46:06 25 4
gpt4 key购买 nike

我正在使用 protobuf 和 gRPC 在 Flutter 应用程序和 python 服务器(Flutter 中的客户端和 python 中的服务器)之间交换信息。服务器在 0.0.0.0 上运行,客户端使用服务器机器的 IP 地址。

import 'dart:async';
import 'User.pbgrpc.dart';
import 'User.pb.dart';
import 'package:grpc/grpc.dart';

Future<Null> main() async {
final channel = new ClientChannel('IP_ADDRESS',
port: 50051,
options: const ChannelOptions(
credentials: const ChannelCredentials.insecure()));
final stub = new StorageClient(channel);

Test input = new Test();
input.id = 1;
try {
var response = await stub.getPerson(input);
print('Greeter client received: ${response}');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}

如果我使用 dart client.dart 运行这个客户端,一切正常,我得到了预期的响应。但是,如果我将此方法嵌入到 flutter 应用程序中,例如:

@override
Widget build(BuildContext context) {

Future<Null> testRPC() async {
final channel = new ClientChannel('IP_ADDRESS',
port: 50051,
options: const ChannelOptions(
credentials: const ChannelCredentials.insecure()));
final stub = new StorageClient(channel);

Test input = new Test();
input.id = 1;
try {
var response = await stub.getPerson(input);
print('Greeter client received: ${response}');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}

testRPC();
...etc
}

我得到:

I/flutter (18824): Caught error: gRPC Error (14, Error connecting: SocketException: OS Error: No route to host, errno = 111, address = localhost, port = 45638)

更新:当我使用模拟器运行该应用程序时,它正在运行。所以这个错误只有在使用真实设备时才会发生。

最佳答案

如果您在同一台计算机上运行 AVD(Client) 和后端,则必须将基本 URL 设置为“10.0.2.2”而不是“localhost/127.0.0.1”。

这是 Github 中的答案.

关于dart - Flutter gRPC 错误 - 操作系统错误 : Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51706409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com