[英]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.