using core.Resource in a Dart transformer leads to Build error


i started with the simple_transformer example on how to write a simple Dart Pub Transformer simple_transformer.
This example defines the content to insert into files by specifying it in code

String copyright = "Copyright (c) 2014, the Example project authors.\n";

instead i wanted to use the new (Dart 1.12) Resource class from the core package in order to load this copyright message from a local file (lib/copyright.txt):

static Future<String> loadCopyright() {
  var copyrightRessource = new Resource("package:simple_resource_loading_transformer/copyright.txt");
  return copyrightRessource.readAsString();

While invoking this method from the main function works

main() {
  print('load copyright.txt');
  //this loads the resource as expected
      (String code)=>print(code)

, invoking it in the Transformer’s apply-method fails when trying to transform another package (which is what Transformers are for). You’ll get a

Build error: Transform InsertCopyright on {your project} threw error: Load Error for “package:simple_resource_loading_transformer/copyright.txt”: SocketException: OS Error: Connection refused

How do i make Resource work in a Pub Transformer? Or is this missing functionality that still should be added to Dart?


so here is the working solution based on the proposed usage of the Transform API

static Future<String> loadCopyright(Transform transform) {
  var copyrightAssetId = new AssetId('simple_resource_loading_transformer', 'lib/copyright.txt');
  return transform.readInputAsString(copyrightAssetId);

The Transform instance comes from the parameter of your Transformer.apply method.



You should really be using the Barback Transform APIs to load assets anyway. That’s what it’s for.

Answered By – Günter Zöchbauer

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.