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

Posted

技术标签:

【中文标题】Dart 错误:错误:当前 Dart 运行时不支持导入 dart:mirrors【英文标题】:Dart Error: error: import of dart:mirrors is not supported in the current Dart runtime 【发布时间】:2020-02-16 17:36:52 【问题描述】:

我目前正在尝试使用 Flutter 编写一些移动代码。我正在尝试使用 gcloud library 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.

【问题讨论】:

Flutter 不支持飞镖镜像 你有什么建议我如何在 Flutter 中从云 pubsub 发布/订阅? 【参考方案1】:

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

【讨论】:

那是救命稻草!!!我花了将近 2 个小时不知道原因 在我的例子中是'package:json_serializable/builder.dart'。谢谢!【参考方案2】:

不幸的是,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 包使用)

【讨论】:

我提出了同样的问题:github.com/dart-lang/gcloud/issues/84

以上是关于Dart 错误:错误:当前 Dart 运行时不支持导入 dart:mirrors的主要内容,如果未能解决你的问题,请参考以下文章

错误:未找到:'dart:ui' import 'dart:ui' - Flutter

Dart / SQFlite导入错误

错误:未找到:'dart:ui' export 'dart:ui' show Offset;

Flutter --version 和运行项目时不兼容的 Dart SDK 版本

Dart 混淆类型错误

如何修复颤振 lib/main.dart 错误