颤动反射与可反射:需要工作示例

时间:2018-07-04 08:58:16

标签: android ios dart flutter

我想使用FlutterReflectable package在跨平台(iOS,Android)项目中实现反射。为了使二进制文件简短,此程序包使用代码生成。

但是,在此软件包的tutorial/readme之后,我无法生成所需的代码,在这种情况下,文件为main.reflectable.dart。在达到一切正常而没有错误的地步时,代码生成以以下语句结束:

[INFO] Succeeded after 88ms with 0 outputs

在下面,我尝试显示我所做的操作的可复制路径。为此,我将flutter移到了其他路径并重新安装,但没有在IntelliJ IDEA中重新安装flutter插件。

如何复制/我做了什么?

I)在Mac上照常安装Flutter 。在命令行上:

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

II)在IntelliJ IDEA中创建一个新的Flutter项目

  1. 选择SDK路径:/ Users /您的姓名/开发/ flutter
  2. 选择项目位置:〜/ gitroot / PlayGround / reflectable_test_2
  3. 添加与lib目录平行的目录entry_point
  4. 在目录entry_point内添加dart文件main.dart
  5. https://github.com/dart-lang/reflectable中的main.dart中获取main.dart的内容(很多都会显示为红色)
  6. 从lib目录中删除main.dart(未选中“安全删除”和“在注释中搜索”)
  7. 在测试目录中删除widet_test.dart
  8. 在依赖项下向pubspec.yaml添加“ reflectable:any”
  9. 在main.dart中,单击“运行”,然后在随后出现的对话框中将入口点设置为/ Users /您的名称/gitroot/PlayGround/reflectable_test_2/entry_point/main.dart

加载依赖项时,一些红色摆动将消失,但“ import'main.reflectable.dart';”中的红色摆动不会消失,因为此文件尚不存在。

III)尝试在命令行中使用生成器生成main.reflectable.dart

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

请注意,本教程只说了最后一行,而不是最后一行

pub run build_runner build DIR

,但使用的行确实是correct when used in a Flutter project。按照到目前为止的自述文件/教程,我得到了结果:

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

IV)在IntelliJ中,在pubspec.yaml中的dev_dependencies中添加“ build_runner:any”。在命令行上再次运行(flutter包pub run build_runner build entry_point)。结果为:

[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

总而言之,没有错误,但也没有创建文件main.reflectable.dart(0个输出)。我该怎么做才能解决此问题?

2 个答案:

答案 0 :(得分:3)

也许唯一要做的事情就是要做

flutter packages pub run build_runner build entry_point/main.dart

或按照以下内容添加build.yaml文件

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

编辑:Here是一个示例存储库,它可能是在Flutter中反映的非常简单的起点。

编辑2:在pub个支持入口点(“ Dart程序”)的位置中,whitelist个位置,而entry_point不在该列表中。尝试使用白名单中存在的目录。

答案 1 :(得分:2)

首先,请确保您已在 pubspec.yaml

中添加了这两个软件包
dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: any  
  built_value_generator: any

仅添加 build_runner 。它不会产生任何结果并最终获得成功。

还要检查您的 main.dart

//reflectable impoort statement should be like this 
import 'main.reflectable.dart' show initializeReflectable;

void main(){

 // initialize Reflectable in main

initializeReflectable();

  runApp(MyApp());
}

现在尝试使用命令进行构建

flutter packages pub run build_runner build DIR

如果它能正常工作,那您真该死。

现在让我们手动添加 build.yaml 文件(与pubspec.yaml相同的目录)

targets:
  $default:
    builders:
      reflectable:
        generate_for:
          - lib/main.dart

现在尝试命令

flutter packages pub run build_runner build DIR