1

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).

deviant
  • 3,539
  • 4
  • 32
  • 47

2 Answers2

2

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.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 24 '21 at 23:43
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30668694) – MD. RAKIB HASAN Dec 26 '21 at 09:12
  • I think that in this solution, you have to implement this Foo function as well. Which is less nice – Rudolf J Sep 29 '22 at 14:50
0

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');

deviant
  • 3,539
  • 4
  • 32
  • 47