Dart Error: error: import of dart:mirrors is not supported in the current Dart runtime

Viewed 8863

I'm currently trying to make some mobile code with Flutter. I'm trying to publish/subscribe data to GCP Cloud Pub/Sub using gcloud library dart. Here is the code for the 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;
}

Here are the denpendencies on the pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter

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

When trying to debug the code I get the following errors:

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

In my case, for one of the projects, I had both 'package:json_annotation/json_annotation.dart' and 'package:json_serializable/json_serializable.dart' imported in the dart file. Removing json_serializable package resolved the above error.

Global search json_serializable .dart (ctrl+shift+f) in your project. If you find any import import 'package:json_serializable/json_serializable.dart' which is not used or unnecessarily imported, remove the import and re-run the project. Hope this may help!

This way fixed my error:

dev_dependencies:
 build_runner: ^1.0.0
 json_serializable: ^3.2.0

 flutter_test:
  sdk: flutter

Actually I put json_serializable dependency on top of flutter dev_dependencies and it worked for me.

For me, I was working on creating my own code generator and I was getting this error every time I tried to run tests on it.

I was trying to run them with flutter test, but I needed to instead use flutter pub run build_runner test.

Side note, make sure to replace all:

import 'package:flutter_test/flutter_test.dart;

with:

import 'package:test/test.dart';

This error occurs when a package uses dart:mirrors in your flutter project.

If you have any imports for example from utils.dart that includes dart:mirrors this error happens.

Dart includes dart:mirrors, which provides type reflection. But since Flutter apps are pre-compiled for production, and binary size is always a concern with mobile apps, this library is unavailable for Flutter apps. (https://docs.flutter.dev/resources/faq#does-flutter-come-with-a-reflection--mirrors-system)

When I had to face this issue, the reason why i had this error, was the import of the following package: package:auto_route_generator/utils.dart

I was creating my own code generation tool and got the next error while running a test: Dart Error: error: import of dart:mirrors is not supported in the current Dart runtime

In my case the error was because Flutter doesn't allow to use Dart's mirrors (for the "tree shaking") and forgot to remove flutter dependency in the 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
Related