Dart 错误:错误:当前 Dart 运行时不支持导入 dart:mirrors

Shi*_*ami 4 dart google-cloud-platform gcloud google-cloud-pubsub flutter

我目前正在尝试使用 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;
}
Run Code Online (Sandbox Code Playgroud)

以下是 pubspec.yaml 上的依赖项:

dependencies:
  flutter:
    sdk: flutter

  googleapis: ^0.54.0
  googleapis_beta: ^0.49.0
  googleapis_auth: ^0.2.10
  gcloud: ^0.6.3

Run Code Online (Sandbox Code Playgroud)

尝试调试代码时,出现以下错误:

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.
Run Code Online (Sandbox Code Playgroud)

小智 12

就我而言,对于其中一个项目,我在 dart 文件中导入了“package:json_annotation/json_annotation.dart”和“package:json_serializable/json_serializable.dart”。删除 json_serializable 包解决了上述错误。


小智 5

json_serializable .dart在项目中进行全局搜索(ctrl+shift+f)。如果您发现任何import 'package:json_serializable/json_serializable.dart' 未使用或不必要的导入,请删除导入并重新运行项目。希望这会有所帮助!


Had*_*ard 2

不幸的是,由于镜像包的原因,dart 的 glcoud 包与 flutter(或 web)不兼容。

我建议您在 github 存储库上提出一个有关在 flutter 上支持 gcloud pub/sub 的问题。 https://github.com/dart-lang/gcloud/issues/new

我还可以检查googleapis包,它有一个可能适合您需求的 pub/sub 实现。 https://pub.dev/documentation/googleapis/latest/googleapis.pubsub.v1/googleapis.pubsub.v1-library.html。(由 glcoud 包使用)