Home > database >  How do I run a command line tool that depends on Flutter?
How do I run a command line tool that depends on Flutter?

Time:01-05

The flutter_test package is able to run Flutter apps 'headless', without requiring any embedder. It would be useful to me to be able to run a command line tool (i.e. not a test) in the same mode. Is this possible? I looked at the flutter_test package but nothing there seems to be the magic solution.

What I've tried:

  • Running a CLI tool that depends on Flutter with dart path/to/tool.dart. This throws Error: Not found: 'dart:ui'.
  • Running it with flutter run path/to/tool.dart. This will attempt to start the CLI tool as a regular Flutter app on a device (such as an Emulator, the desktop OS, or a web browser).
  • Running with flutter pub run path/to/tool.dart just generates a lot of errors, such as "Error: 'Paint' isn't a type". I tried all approaches suggested in this related SO Question, with the script living in various directories and using different invocations, but no luck.
  • Running the tool using flutter test path/to/tool.dart does work, and it runs the code in main() like one would expect. But I consider this a hacky workaround, since flutter test (unsurprisingly) expects a test, and exits with the error "No tests were found." And, semantically, it's just wrong. This is not a test. It's a tool.

CodePudding user response:

If your code relies on package:flutter or dart:ui, you won't be able to run it outside of a Flutter engine instance (e.g., flutter_tester or a Flutter application) as dart:ui is a custom core library for Flutter that isn't part of Dart's core libraries. Short of hacking away at flutter_tester to remove the test runner related logic or creating your own custom embedder of the Flutter engine, you won't be able to run a Flutter project headless on the command line.

Ideally, you'd just refactor your tool to not depend on dart:ui at all, but given your background I'm guessing you're trying to do something non-standard that actually requires functionality from the Flutter framework... :-)

  • Related