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
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?
So reflectable-the-library just runs regular Dart code, it doesn't change the execution of a Flutter app, and you could have written all that generated code yourself (but you'd get bored ;-).
What is Flutter Reflection with Reflectable in Dart? Free and open-source, Flutter is a framework for creating native-looking applications for both iOS and Android. When it comes to developing mobile apps that are both speedy and functional, Flutter is an excellent tool for any Flutter agency to have on their side!
In general, reflection is provided via a subclass of the class Reflectable (we use the term reflector to designate an instance of such a subclass). class Reflector extends Reflectable { const Reflector () : super(capability1, capability2, ...); } Reflection is disabled by default, and it is enabled by specifying reflectable capabilities.
With reflectable, you don't have to annotate a class a priori in order to get reflection support for it. You can see an example in this test where the class Mark is associated with the reflection support specified using reflector, and we could have used any class we want in place of Mark.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With