1

我正在尝试使用 Flutter 作为前端构建 Web 应用程序,使用后端和 gRPC 作为通信协议。为了避免在 2 个不同的目录中使用相同的 protobuf,我重构了 protobuf 并将它们编译到客户端和服务器文件夹之外的第三个目录(都在同一个 repo 中)。现在我无法从客户端代码访问已编译的 dart protobufs。这是我的文件夹结构:

.
|-- client/
  |-- pubspec.yaml
|-- protos/
  |-- dart_protos/
    |-- .dart_tool/
    |-- .packages
    |-- cards.pb.dart
    |-- cards.pbenum.dart
    |-- cards.pbjson.dart
    |-- cards.pbserver.dart
    |-- pubspec.lock
    |-- pubscpec.yaml
  |-- go_protos/
  |-- cards.proto

|-- server/

这是我在我的client/pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  recase: ^4.0.0
  search_choices: ^2.0.7
  tcb_protos:
    path: ../protos/dart_protos

我的protos/dart_protos/pubspec.yaml

name: tcb_protos
environment:
  sdk: ">=2.0.0-dev.68.0 <3.0.0"
dependencies:
  protobuf: ^1.1.4
  protoc_plugin: ^19.3.1

以及我如何导入 protobuf:

import 'package:tcb_protos/cards.pb.dart';

所以我遇到了几个问题:

  1. 我正在Target of URI hasn't been generated: 'package:tcb_protos/cards.pb.dart'.进口
  2. 我无法使用基于导入的 protobuf,并且 VSCode 的智能感知不适用于包(我假设这是因为包定义或导入错误)

如何在 Flutter 小部件中使用 protobuf?

4

1 回答 1

1

您需要将原始.pb.dart文件放在lib/文件夹中。

我在这里做了一个小例子:https ://github.com/sigurdm/shared_proto_example

如果您想在相互引用的不同包中拥有原型,目前不支持:https ://github.com/google/protobuf.dart/issues/295

于 2021-10-01T07:21:19.927 回答