deviant
deviant

Reputation: 3711

How to check if flutter app runs in standalone mode or as a module inside of a native app?

I'm developing a flutter module for an existing native android app. I made some interop with MethodChannel. Now, for test purposes, I would like to run my module as a standalone app. To do so I mocked all my interop code with dummy placeholders. Now I want to check (programmatically) if flutter runs in standalone mode or is a part of a module, to make a decision on which interop implementation to use (the android one VS the dummy).

Upvotes: 1

Views: 605

Answers (2)

Andrew Zhao
Andrew Zhao

Reputation: 23

My solution is when the app start, try to invoke a method in MethodChannel, if throws MissingPluginException means flutter runs standalone.

/// check if flutter is run as a moudle embeded in a app project
static Future<bool> get isEmbeded async {
  try {
    await _methodChannel.invokeMethod('Foo');
    return true;
  } on MissingPluginException {
    return false;
  }
}

Call this when start, and you can save the result somewhere as a global static variable to avoid future async calls.

Upvotes: 2

deviant
deviant

Reputation: 3711

I ended up with this solution so far:

flutter run --dart-define="standalone=true"

Using it when building a standalone app. Then in the code:

const bool isStandalone = bool.fromEnvironment('standalone');

Upvotes: 1

Related Questions