Skip to content

Error when running a headless FlutterEngine in an iOS App Extension #165904

Open

Description

@AlexNik4

Can a headless FlutterEngine work in iOS App Extensions? I'm trying to share Dart code between my main app and an iOS network extension via a Flutter module by loosely following Adding iOS app extensions and Integrate a Flutter module into iOS

When initializing the FlutterEngine and trying to invoke a MethodChannel in my Extension Service:

let engine = FlutterEngine(name: "extension", project: nil, allowHeadlessExecution: true)
engine.run(withEntrypoint: "main")*
// Create MethodChannel

I get this error:

Modifying properties of a view's layer off the main thread is not allowed: view <UIView: 0x104a57b80> with no associated or ancestor view controller;*

Is this architecture possible? The error suggests Flutter is still trying to create UI components despite running headless.

Addition Details

I have a large amount of Dart code that needs to be reused in my Push Provider Extension. A lot of it is pure Dart, so I can potentially at least reuse that using ffi(I think). But a good chunk uses Flutter as well (local notifications, audio, local storage). It would be awesome to reuse all of it in the iOS extension. I was hoping that running the FlutterEngine without the UI would create a smaller footprint and still allow access to the Flutter SDK. I really can't find any direct documentation around this.

Activity

  1. darshankawar commented on Mar 26, 2025

    @darshankawar
    Member

    @AlexNik4
    Check if this maybe is related / similar to your case or not.

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Mar 26, 2025
  3. AlexNik4 commented on Mar 27, 2025

    @AlexNik4
    Author

    @darshankawar Thanks for your response! Since the approach isn’t outright impossible, I tested further in a minimal sample project using a Packet Tunnel Network Extension (to avoid App Push Provider entitlement requirements). I ended up avoiding any additional plugins for now.

    Current Setup in the Extension Service

    FlutterEngine Initialization (Swift):
    self.flutterEngine = FlutterEngine(  
        name: "extension_engine",  
        project: nil,  
        allowHeadlessExecution: true  
    )  
    flutterEngine.run(  
        withEntrypoint: "mainHeadless",  
        libraryURI: "package:flutter_ios_extension/headless.dart"  
    

    Confirmed the entrypoint is correct (misnamed withEntrypoint or libraryURI throw Dart_LookupLibrary errors).

    headless.dart Entrypoint:

    @pragma('vm:entry-point')  
    void mainHeadless() {  
      WidgetsFlutterBinding.ensureInitialized();  
      const _channel = MethodChannel('com.example.flutterIosExtension/my_channel');  
    
      _channel.setMethodCallHandler((call) async {  
        switch (call.method) {  
          case 'getData':  
            return "Hello from Headless Flutter!";  
          default:  
            throw PlatformException(code: 'UNSUPPORTED_METHOD');  
        }  
      });  
      runApp(const SizedBox.shrink());  
    } 
    

    Issues Observed

    1. Still seeing the threading violation (despite no UI):
    Modifying properties of a view's layer off the main thread is not allowed:  
    view <UIView: 0x104a57b80> with no associated or ancestor view controller 
    
    1. Silent MethodChannel Failure:
    methodChannel?.invokeMethod("getData", arguments: nil) { response in  
        NSLog("XXXX Received: \(response ?? "")")  // No output or errors  
    } 
    

    The Dart handler is never triggered.

    • Any working examples of headless Flutter + MethodChannel in this context?
    • Any documentation on this use case?
      I’d greatly appreciate any guidance—thanks again for your time!
    Doctor summary (to see all details, run flutter doctor -v):
    [✓] Flutter (Channel stable, 3.29.2, on macOS 15.3.2 24D81 darwin-arm64, locale en-US)
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    [✓] Xcode - develop for iOS and macOS (Xcode 16.2)
    [✓] Chrome - develop for the web
    [✓] Android Studio (version 2023.1)
    [✓] VS Code (version 1.98.2)
    [✓] Connected device (4 available)
    [✓] Network resources
    
    • No issues found!
    
  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Mar 27, 2025
  5. darshankawar commented on Mar 28, 2025

    @darshankawar
    Member

    Thanks for the update. I think from Android side, something like workmanager would probably work, but I am not sure if there's equivalent for iOS, so I'll keep the issue open for team's tracking.

  6. added
    platform-iosiOS applications specifically
    d: api docsIssues with https://api.flutter.dev/
    team-iosOwned by iOS platform team
    and removed
    in triagePresently being triaged by the triage team
    on Mar 28, 2025
  7. hellohuanlin commented on Apr 18, 2025

    @hellohuanlin
    Contributor

    From reading FlutterEngine.mm, there are quite a few places that could have been skipped for headless mode. For example, text input plugin setup (which involves creating a few UIViews) isn't necessary for headless mode.

  8. added
    P2Important issues not at the top of the work list
    triaged-iosTriaged by iOS platform team
    on May 15, 2025
  9. dyaboykyl commented on Nov 14, 2025

    @dyaboykyl

    Hello, just chiming in that this would be really great to see fixed. We would like to run some business logic in flutter from an extension and that logic is not UI-related.

  10. Gibbo97 commented on Jan 29, 2026

    @Gibbo97
    Contributor

    We have been trying to run the flutter engine in a Notification Service Extension. Although I believe the changes needed are the same.

    Have made a PR #181645

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listd: api docsIssues with https://api.flutter.dev/d: examplesSample code and demosplatform-iosiOS applications specificallyteam-iosOwned by iOS platform teamtriaged-iosTriaged by iOS platform team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions