我目前正在尝试使用 Flutter 编写一些移动代码。我正在尝试使用 gcloud 库 dart 将数据发布/订阅到 GCP Cloud Pub/Sub。这是 main.dart 的代码:
import 'dart:io';
import 'package:googleapis_auth/auth_io.dart' as auth;
import 'package:http/http.dart' as http;
import 'package:gcloud/db.dart';
import 'package:gcloud/storage.dart';
import 'package:gcloud/pubsub.dart';
import 'package:gcloud/service_scope.dart' as ss;
import 'package:gcloud/src/datastore_impl.dart' as datastore_impl;
Future<int> main() async {
// Read the service account credentials from the file.
var jsonCredentials = new File('path/file.json').readAsStringSync();
var credentials = new auth.ServiceAccountCredentials.fromJson(jsonCredentials);
// Get an HTTP authenticated client using the service account credentials.
var scopes = []
..addAll(PubSub.SCOPES);
var client = await auth.clientViaServiceAccount(credentials, scopes);
// Instantiate objects to access Cloud Datastore, Cloud Storage
// and Cloud Pub/Sub APIs.
var pubsub = new PubSub(client, 'project-name');
ss.fork(() {
// register the services in the new service scope.
registerPubSubService(pubsub);
// Run application using these services.
});
var topic = await pubsub.createTopic('flutter');
await topic.publishString('coba publish dr flutter');
var subscription =
await pubsub.createSubscription('sub_flutter', 'flutter');
var pullEvent = await subscription.pull();
print(pullEvent.message.asString);
await pullEvent.acknowledge();
return 0;
}
以下是 pubspec.yaml 上的依赖项:
dependencies:
flutter:
sdk: flutter
googleapis: ^0.54.0
googleapis_beta: ^0.49.0
googleapis_auth: ^0.2.10
gcloud: ^0.6.3
尝试调试代码时出现以下错误:
Launching lib\main.dart on vivo 1918 in debug mode...
Built build\app\outputs\apk\debug\app-debug.apk.
E/flutter (20586): [ERROR:flutter/shell/common/shell.cc(178)] Dart Error: error: import of dart:mirrors is not supported in the current Dart runtime
E/flutter (20586): [ERROR:flutter/shell/common/engine.cc(188)] Could not prepare to run the isolate.
E/flutter (20586): [ERROR:flutter/shell/common/engine.cc(127)] Engine not prepare and launch isolate.
E/flutter (20586): [ERROR:flutter/shell/common/shell.cc(407)] Could not launch engine with configuration.
就我而言,对于其中一个项目,我在 dart 文件中导入了“package:json_annotation/json_annotation.dart”和“package:json_serialized/json_serialized.dart”。删除 json_serialized 包解决了上述错误。
全局搜索
json_serializable .dart
(ctrl+shift+f)。如果您发现任何未使用或不必要导入的导入import 'package:json_serializable/json_serializable.dart'
,请删除导入并重新运行项目。
希望这会有所帮助!
不幸的是,由于镜像包的原因,dart 的 glcoud 包与 flutter(或 web)不兼容。
我建议您在 github 存储库上提出一个有关在 flutter 上支持 gcloud pub/sub 的问题。 https://github.com/dart-lang/gcloud/issues/new
我还可以检查 googleapis 包,它有一个可能满足您需求的发布/订阅实现。 https://pub.dev/documentation/googleapis/latest/googleapis.pubsub.v1/googleapis.pubsub.v1-library.html。 (由 glcoud 包使用)
这样解决了我的错误:
dev_dependencies:
build_runner: ^1.0.0
json_serializable: ^3.2.0
flutter_test:
sdk: flutter
实际上,我将
json_serializable
依赖放在 flutter dev_dependency 之上,它对我有用。
对我来说,我正在创建自己的代码生成器,每次尝试对其运行测试时都会收到此错误。
我试图用
flutter test
运行它们,但我需要改用 flutter pub run build_runner test
。
旁注,请确保全部替换:
import 'package:flutter_test/flutter_test.dart;
与:
import 'package:test/test.dart';
当包在您的 flutter 项目中使用
dart:mirrors
时,会出现此错误。
如果您有任何导入,例如来自
utils.dart
的包含 dart:mirrors 的导入,则会发生此错误。
Dart 包含 dart:mirrors,它提供类型反射。但由于 Flutter 应用程序是针对生产环境进行预编译的,并且二进制大小始终是移动应用程序关注的一个问题,因此该库不适用于 Flutter 应用程序。 (https://docs.flutter.dev/resources/faq#does-flutter-come-with-a-reflection--mirrors-system)
当我不得不面对这个问题时,我遇到这个错误的原因是导入了以下包:
package:auto_route_generator/utils.dart
我正在创建自己的代码生成工具,并在运行测试时遇到下一个错误:
Dart Error: error: import of dart:mirrors is not supported in the current Dart runtime
在我的例子中,错误是因为 Flutter 不允许使用 Dart 的镜像(用于“tree shake”)并且忘记删除
flutter
中的 pubspec.yaml
依赖:
dependencies:
flutter: # <----------- remove this line
sdk: flutter # <----------- remove this line
meta: ^1.7.0
analyzer: ^4.7.0
build: ^2.3.1
source_gen: ^1.2.5
我添加了新包“class_to_map”。 然后我得到“错误:错误:当前 Dart 运行时不支持导入 dart:mirrors”。 您需要删除最后一个包。
当我开发库来生成源代码时,我也遇到了这个问题。我的错误是将
generator
和 annotations
都放在同一个项目中。因此,我必须将库添加为 dependencies
并通过将生成器添加到 dependencies
而不是 dev_dependencies
我遇到了这个问题,该问题阻止应用程序在测试和运行时工作。因此,解决方案是将 annotations
和 generator
彼此分开,并将 annotations
添加到 dependencies
,将 generator
添加到 dev_dependencies
。目录结构:
my_library:
generator/
// contains build.yaml , generators , source_gen , build , ...
annotations/
// contains annotations class
example/
// pubspec.yaml :
dependencies:
flutter:
sdk: flutter
annotations:
path: ../annotations/
cupertino_icons: ^1.0.6
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: 2.3.3
generator:
path: ../generator/
希望它可以帮助那些在基于
source_gen
和 annotations
创建源生成器库时遇到此问题的人