Is there a way to run a single file with a main function without having to compile the entire app?
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.
Can I run one file in a project or should I use dartpad or a new project to do so?
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:
/// 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. Flutter already uses this technique for unit test (u can check the test folder), each test file has its own main()
function.