Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Reflection with Reflectable: working example needed

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?

like image 259
CryptUser Avatar asked Jul 04 '18 08:07

CryptUser


People also ask

Does reflectable-the-library work with flutter?

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?

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!

How do I enable reflection in Java?

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.

Do you have to annotate a class to get reflection support?

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.


Video Answer


1 Answers

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.

like image 67
Erik Ernst Avatar answered Sep 25 '22 10:09

Erik Ernst