Flutter Reflection with Reflectable: working examp

2019-07-29 12:50发布

问题:

I want to achieve reflection in a cross-platform (iOS, Android) project using Flutter and the Reflectable package. To keep the binaries short, this package uses code generation.

However, following the tutorial/readme of this package, I am not able to generate the needed code, in this case the file main.reflectable.dart. While I have reached the point where things work without error, code generation concludes with the statement:

[INFO] Succeeded after 88ms with 0 outputs

In the following I try to show a reproducible path of what I did. For that I moved flutter to a different path and reinstalled it, but didn't reinstall the flutter plugin in IntelliJ IDEA.

How to reproduce / What I did?

I) Install Flutter as usual for Mac. On the command line:

cd ~/development 
git clone -b beta https://github.com/flutter/flutter.git 
export PATH=/Users/yourname/development/flutter/bin:$PATH 
flutter doctor

II) Create a new Flutter project in IntelliJ IDEA

  1. Choose SDK path: /Users/yourname/development/flutter
  2. Choose project location: ~/gitroot/PlayGround/reflectable_test_2
  3. Add directory entry_point parallel to lib directory
  4. Add dart file main.dart inside directory entry_point
  5. Get the content for main.dart from the main.dart in https://github.com/dart-lang/reflectable (a lot will be shown red)
  6. Delete main.dart from lib directory (unchecked "safe delete" and "search in comments")
  7. Delete widet_test.dart in test directory
  8. Add "reflectable: any" to pubspec.yaml under dependencies
  9. In main.dart, click run and in the upcoming dialog set the entry point to /Users/yourname/gitroot/PlayGround/reflectable_test_2/entry_point/main.dart

When the dependencies are loaded, some of the red wiggles will go away, but not the one in "import 'main.reflectable.dart';", since this file does not exist yet.

III) Try to generate main.reflectable.dart with the builder on the command line:

cd /Users/yourname/gitroot/PlayGround/reflectable_test_2/
flutter packages pub run build_runner build entry_point

Note that instead of the last line, the tutorial only says

pub run build_runner build DIR

but the used line is indeed correct when used in a Flutter project. Following the readme/tutorial so far, I got the result:

Package "build_runner" is not an immediate dependency.
Cannot run executables in transitive dependencies.
pub finished with exit code 65

IV) In IntelliJ, add "build_runner: any" to dev_dependencies in pubspec.yaml. Run again on the command line (flutter packages pub run build_runner build entry_point). This results in the output:

[INFO] Generating build script...
[INFO] Generating build script completed, took 506ms

[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 776ms

[INFO] Checking for unexpected pre-existing outputs....
[INFO] Checking for unexpected pre-existing outputs. completed, took 3ms

[INFO] Running build...
[INFO] Running build completed, took 7ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 69ms

[INFO] Succeeded after 88ms with 0 outputs

To summarise, no errors, but it didn't create the file main.reflectable.dart either (0 outputs). What can I do to fix this?

回答1:

Maybe the only missing bit is to do

flutter packages pub run build_runner build entry_point/main.dart

or add a build.yaml file along the lines of

targets: test_reflectable: builders: reflectable: generate_for: - entry_point/main.dart

Edit: Here's an example repo which may serve as a very simplistic starting point for reflectable in Flutter.

Edit 2: There is a whitelist of locations where pub supports entry points ("Dart programs"), and entry_point is not on that list. Try using a directory which is present in the whitelist.