protobuf dart 编译器生成的代码有很多错误(方法未找到或不是类型)

protobuf dart compiler generates code with lots of errors (Method not found or Is not a type)

我将一些 protobuf 代码编译为 C++,它运行良好,但对于 Dart(使用截至今天第 6 天的最新提交),我在尝试使用生成的代码编译我的项目时遇到很多错误。

例如,这个文件:


syntax = "proto3";

import "stream_setup.proto";
import "uuid.proto";
package message;

message Collection {
    UUID uuid = 1;
    string name = 2;
    //Each entry should represent the order in which the StreamSetup appears
    //in the collection. Since it's a map it can't have repeated entries
    map<sint32, StreamSetup> streams = 3;
}

生成此文件:https://pastebin.com/HK70SfGv,在我的 Dart 项目中编译时,给出:

                                     ^^^^^^^^^                         
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:16:20: Error: 'BuilderInfo' isn't a type.
  static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'Collection', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'message'), createEmptyInstance: create)
                   ^^^^^^^^^^^                                          
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:16:157: Error: Method not found: 'PackageName'.
  static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'Collection', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'message'), createEmptyInstance: create)
                                                                                                                                                            ^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:16:37: Error: Method not found: 'BuilderInfo'.
  static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'Collection', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'message'), createEmptyInstance: create)
                                    ^^^^^^^^^^^                         
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:19:180: Error: Getter not found: 'PbFieldType'.
    ..m<$core.int, .StreamSetup>(3, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'streams', entryClassName: 'Collection.StreamsEntry', keyFieldType: $pb.PbFieldType.OS3, valueFieldType: $pb.PbFieldType.OM, valueCreator: .StreamSetup.create, packageName: const $pb.PackageName('message'))
                                                                                                                                                                                   ^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:19:217: Error: Getter not found: 'PbFieldType'.
    ..m<$core.int, .StreamSetup>(3, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'streams', entryClassName: 'Collection.StreamsEntry', keyFieldType: $pb.PbFieldType.OS3, valueFieldType: $pb.PbFieldType.OM, valueCreator: .StreamSetup.create, packageName: const $pb.PackageName('message'))
                                                                                                                                                                                                                        ^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:19:293: Error: Method not found: 'PackageName'.
    ..m<$core.int, .StreamSetup>(3, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'streams', entryClassName: 'Collection.StreamsEntry', keyFieldType: $pb.PbFieldType.OS3, valueFieldType: $pb.PbFieldType.OM, valueCreator: .StreamSetup.create, packageName: const $pb.PackageName('message'))
                                                                                                                                                                                                                                                                                                    ^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:25:63: Error: 'ExtensionRegistry' isn't a type.
  factory Collection.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
                                                              ^^^^^^^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:25:89: Error: Getter not found: 'ExtensionRegistry'.
  factory Collection.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
                                                                                        ^^^^^^^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:25:128: Error: The method 'mergeFromBuffer' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named 'mergeFromBuffer'.
  factory Collection.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
                                                                                                                               ^^^^^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:26:52: Error: 'ExtensionRegistry' isn't a type.
  factory Collection.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
                                                   ^^^^^^^^^^^^^^^^^    
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:26:78: Error: Getter not found: 'ExtensionRegistry'.
  factory Collection.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
                                                                             ^^^^^^^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:26:117: Error: The method 'mergeFromJson' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named 'mergeFromJson'.
  factory Collection.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
                                                                                                                    ^^^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:31:39: Error: The method 'mergeFromMessage' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named 'mergeFromMessage'.
  Collection clone() => Collection()..mergeFromMessage(this);           
                                      ^^^^^^^^^^^^^^^^                  
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:36:67: Error: Superclass has no method named 'copyWith'.
  Collection copyWith(void Function(Collection) updates) => super.copyWith((message) => updates(message as Collection)); // ignore: deprecated_member_use
                                                                  ^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:41:53: Error: Method not found: 'PbList'.
  static $pb.PbList<Collection> createRepeated() => $pb.PbList<Collection>();
                                                    ^^^^^^              
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:43:62: Error: Getter not found: 'GeneratedMessage'.
  static Collection getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<Collection>(create);
                                                             ^^^^^^^^^^^^^^^^
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:47:23: Error: The method '$_getN' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named '$_getN'.
  [=11=].UUID get uuid => $_getN(0);                                        
                      ^^^^^^                                            
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:49:25: Error: The method 'setField' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named 'setField'.
  set uuid([=11=].UUID v) { setField(1, v); }                               
                        ^^^^^^^^                                        
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:51:27: Error: The method '$_has' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named '$_has'.
  $core.bool hasUuid() => $_has(0);                                     
                          ^^^^^                                         
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:53:23: Error: The method 'clearField' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named 'clearField'.
  void clearUuid() => clearField(1);                                    
                      ^^^^^^^^^^                                        
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:55:27: Error: The method '$_ensure' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named '$_ensure'.
  [=11=].UUID ensureUuid() => $_ensure(0);                                  
                          ^^^^^^^^                                      
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:58:28: Error: The method '$_getSZ' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named '$_getSZ'.
  $core.String get name => $_getSZ(1);                                  
                           ^^^^^^^                                      
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:60:30: Error: The method '$_setString' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named '$_setString'.
  set name($core.String v) { $_setString(1, v); }                       
                             ^^^^^^^^^^^                                
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:62:27: Error: The method '$_has' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named '$_has'.
  $core.bool hasName() => $_has(1);                                     
                          ^^^^^                                         
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:64:23: Error: The method 'clearField' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named 'clearField'.
  void clearName() => clearField(2);                                    
                      ^^^^^^^^^^                                        
../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart:67:55: Error: The method '$_getMap' isn't defined for the class 'Collection'.
 - 'Collection' is from 'package:liborwellprofile2/collection.pb.dart' ('../liborwell/common/liborwellprofile2/proto_dart/lib/collection.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named '$_getMap'.
  $core.Map<$core.int, .StreamSetup> get streams => $_getMap(2);      
             

我不知道发生了什么。它应该可以工作,因为我没有修改生成的文件中的任何内容,我只是将它们放在一个包中并加载到我的项目中。

我解决了我的问题。

我必须添加那些库

将 .proto 转换为 dart

https://pub.dev/packages/protoc_plugin

转换后显示很多错误并添加了这些库。

fixnum: ^1.0.0
protobuf: ^2.0.0

添加后仍然显示一些错误,但现在导入无效

import '../../../DartFile.pbenum.dart';
export '../../../DartFile.pbenum.dart';

我将手动生成的文件更改为,因为它们在同一个文件夹中。我不知道为什么会像上面那样生成

import 'DartFile.pbenum.dart';
export 'DartFile.pbenum.dart';