简体   繁体   English

如何使用 Android Studio 在您的 Flutter 项目中运行单个 Dart 文件?

[英]How to run a single Dart file in your Flutter project using Android Studio?

Is there a way to run a single file with a main function without having to compile the entire app?有没有一种方法可以运行带有主 function 的单个文件而无需编译整个应用程序?

I want to be able to test snippets of code, even when the projects as a whole is not compiling.我希望能够测试代码片段,即使整个项目没有编译。

I have created a main_scraps.dart file with a main() method in it and target this file when running, but since it compiles the entire project it will never run.我创建了一个main_scraps.dart文件,其中包含一个main()方法,并在运行时以该文件为目标,但由于它编译了整个项目,因此它永远不会运行。

Can I run one file in a project or should I use dartpad or a new project to do so?我可以在项目中运行一个文件还是应该使用 dartpad 或新项目来这样做?

For example, there is a file for data seeding and it is run on-demand in the terminal, independently and isolated from the main() function of the entire Flutter app:例如,有一个数据播种文件,它在终端按需运行,独立于整个 Flutter 应用程序的main() function 隔离:

/// This file is at lib/seeding/seed_services.service.dart  
import 'dart:convert';

import 'package:users/services/gmp_service/find_nearby_places.service.dart';

seedServices() async {
  print(jsonEncode(await findNearbyPlaces()));
}

// Any dart program must have a main() function.
main() {
  seedServices();
}

In the command line, run the following command to execute the file:在命令行中,运行以下命令来执行文件:

dart lib/seeding/seed_services.service.dart

U can have multiple independent files in the same root directory of a Flutter project to seed different data, each file with its own main() function. U 可以在 Flutter 项目的同一根目录中拥有多个独立文件来播种不同的数据,每个文件都有自己的main() function。 Flutter already uses this technique for unit test (u can check the test folder), each test file has its own main() function. Flutter 已经使用这种技术进行单元测试(你可以检查测试文件夹),每个测试文件都有自己的main() function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Dart 或 Flutter 项目中重构 Android Studio 中的文件名? - How to refactor file names in Android Studio in Dart or Flutter project? 如何使用单个 flutter 项目、飞镖/颤振对 ios 和 android 进行不同的应用 - how to make different application for ios and android using single flutter project, dart/flutter 如何从 Flutter 项目中运行选定的 dart 文件作为主要文件? - How to run selected dart file as main from Flutter project? 如何在 Android Studio 和 IntelliJ 中安装 flutter 和 dart? - How to install flutter and dart in Android Studio and InttelliJ? Flutter,android工作室。 main.dart 文件每次运行都会打开 - Flutter, android studio. main.dart file opens every run 如何在Android Studio中启动Dart项目? - How to start Dart project in Android Studio? android studio/flutter/dart:如何 select dart ZEAE18BC41E1434DD98FA2DADD98Z93? - android studio/flutter/dart: how to select the dart sdk? Android Studio 将 Dart spread 运算符标记为 flutter 项目中的错误 - Android Studio marks Dart spread operator as an error in flutter project Flutter项目,一些文件名在Android Studio中没有被识别为.dart文件 - Flutter project, some filenames not identified as .dart files in Android Studio 关于格式化 Android Studio 在 Flutter 项目中展开 Dart 代码 - On Formatting Android Studio unfolds Dart code in a Flutter project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM