如何从 Flutter 包/库中运行示例?

Mar*_*rcG 6 flutter flutter-test flutter-dependencies

我有以下简单的目录结构:

flutter_published
    .idea
    android
    build
    ios
    lib
        main.dart 
    flutter_published.iml
    pubspec.lock
    pubspec.yaml
    network_to_file_image
        .idea
        example
            main.dart 
        lib
            network_to_file_image.dart
        test
        network_to_file_image.iml
        pubspec.lock
        pubspec.yaml
Run Code Online (Sandbox Code Playgroud)

network_to_file_image 是一个包。

有两个main.dart files,一个在flutter_published/lib/main.dart,另一个在flutter_published/network_to_file_image/example/main.dart

我能够跑第一个,但不是的一个内部example目录下network_to_file_image。第二个给了我这个错误:

   Launching example\lib\main.dart on Android SDK built for x86 in debug mode...
   No application found for TargetPlatform.android_x86.
   Is your project missing an android\AndroidManifest.xml?
   Consider running "flutter create ." to create one.
Run Code Online (Sandbox Code Playgroud)

此外,当应用程序生成时,我使用的包的exampletest目录会发生什么?它们是包含在部署的最终应用程序中还是从部署的应用程序中删除?

Mar*_*rcG 5

为了解决这个问题,你需要main.dartexample目录内部创建一个完整的 Flutter 应用程序类型的项目,而不是example目录里面的文件。然后,示例选项卡将指向README.md该目录中的文件。

example目录将有自己的lib目录,其中包含一个main.dart文件。由于该文件现在位于应用程序类型目录中,因此可以运行它。

访问这个 repo 看看它是如何工作的:

https://github.com/marcglasberg/async_redux/tree/master/example


更新:

需要明确的是,示例pubspec.yaml文件可以通过使用相对引用来引用其包。例如,这里是我提到的包dependenciesexample目录部分async_redux

dependencies:
  http: ^0.13.1
  async_redux:
    path: ../
  flutter:
    sdk: flutter
Run Code Online (Sandbox Code Playgroud)

由于example目录与包的pubspec.yaml文件处于同一级别,因此示例自己pubspec.yaml的目录比它低一级。因此,它可以通过使用../路径来引用包本身:

  async_redux:
    path: ../
Run Code Online (Sandbox Code Playgroud)