Skip to content

wolfenrain/flutter_daemon

Repository files navigation

flutter_daemon

Pub ci coverage style: very good analysis License: MIT


A programmable interface around the Flutter daemon protocol.


The flutter command-line tool supports running as a daemon server, allowing developers to control the running flutter application by using the built-in JSON-RPC communication protocol.

This package provides a programmable interface on top of that to allow developers to use the daemon with a strongly typed API. It maps all the documented method that the daemon supports and allows developers to listen to events emitted by the daemon.

Installation

Add flutter_daemon as a dependency to your pubspec.yaml file (what?).

You can then import the Flutter daemon:

import 'package:flutter_daemon/flutter_daemon.dart';

Usage

You can start using the Flutter daemon by creating an instance of the daemon and running a Flutter application:

import 'package:flutter_daemon/flutter_daemon.dart';

void main() async {
  final daemon = FlutterDaemon();

  daemon.events.listen((event) {
    // Listen to events emitted by the daemon.
  });

  // You can run an application from the daemon.
  // Or alternatively you can attach using `daemon.attach`
  final application = daemon.run(
    arguments: [
      // Any flutter arguments go here.
    ],
    workingDirectory: 'your/flutter/app/location/',
  );


  application.events.listen((event) {
    // Listen to events specifically emitted by this application.
  });

  // Restart the application
  await application.restart();

  // Detach from the application, but don't stop it.
  await application.detach();

  // Stop the application.
  await application.stop();
}

Calling Service Extensions

You can call both Flutter and Dart service extensions using the daemon as well:

void main() async {
  final daemon = FlutterDaemon();

  final application = daemon.run(
    /// ...
  );

  final response = await application.callServiceExtension('ext.myExtension.handler', {
    'some': 'parameters'
  });

  if (response.hasError) throw response.error;
  print(response.result);
}

Reusing the daemon

Once you run or attach an application the daemon can't be reused until you close it's current process:

void main() async {
  final daemon = FlutterDaemon();

  final application1 = daemon.run(
    // ...
  );

  // If you don't close it and try to run or attach again it will throw a StateError.
  await daemon.close();

  final application2 = daemon.attach(
    // ...
  );
}

Once you are completely done with the daemon make sure you dispose of it correctly:

void main() async {
  final daemon = FlutterDaemon();

  // ...

  await daemon.dispose();
}

Contributing

Have you found a bug or have a suggestion of how to enhance Goose? Open an issue and we will take a look at it as soon as possible.

Do you want to contribute with a PR? PRs are always welcome, just make sure to create it from the correct branch (main) and follow the checklist which will appear when you open the PR.