根据是否为 Web 构建 grpc 通道所需的包

功能

  • 该实用程序有条件地为 flutter web 或其他平台构建 gRPC 通道,具体取决于运行时环境

用法

const webAppPort = 8888;
const someGrpcServicePort = 5555;
const host = 'http://127.0.0.1';

// for web I suggest to use Envoy as a proxy routing from webAppPort to someGrpcServicePort
int get port => kIsWeb ? webAppPort : someGrpcServicePort;

class SomeGrpcService {
  late final SomeGrpcClient stub;

  SomeGrpcService() {
    init();
  }

  void init() {
    final channel = buildGrpcChannel(host: host, port: port, secure: false);
    stub = SomeGrpcClient(channel);
  }

  Future<SomeRpcResponse> someRpc(SomeRpcRequest request) {
    final response = stub.sendQuestion(request);
    return response;
  }
}

GitHub

查看 Github