【问题标题】:Import and use protobufs outside of lib directory - dart在 lib 目录之外导入和使用 protobufs - dart
【发布时间】:2021-11-21 00:32:44
【问题描述】:

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

.
|-- 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?

【问题讨论】:

    标签: flutter dart protocol-buffers grpc-dart


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 2014-02-05
      • 2021-12-17
      • 2019-11-10
      • 2016-05-25
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多