Skip to content

[iOS] regression: Hot Restart with a Platform View on a Navigator push throws PlatformException "recreating_view" #163935

@PaulAllanSturm

Description

@PaulAllanSturm

Steps to reproduce

flutter create a_platform_view --platforms=ios --template=plugin
edit example/lib/main.dart -- Navigator push a UiKitView
edit plugin swift code -- a simple PlatformView with a UIView/UILabel
flutter run
Tap "Open" to push the UiKitView
"R" to Hot Restart
Tap "Open" to push the UiKitView
=>

[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: PlatformException(recreating_view, trying to create an already created view, view id: '0', null)

This worked in 3.24.3 (the code works as expected with no PlatformException). It fails in 3.27.2 and on current main, though I haven't tracked down the precise build where it started failing.

Expected results

Hot Restart should restore the state of the app so it can run fresh.

Actual results

When you press the "Open" button after the Hot Restart (which pushes a new UiKitView), an exception is thrown.

Performing hot restart...                                               
Restarted application in 306ms.
[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: PlatformException(recreating_view, trying to create an already created view, view id: '0', null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:646:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
<asynchronous suspension>
#2      PlatformViewsService.initUiKitView (package:flutter/src/services/platform_views.dart:248:5)
<asynchronous suspension>
#3      _DarwinViewState._createNewUiKitView (package:flutter/src/widgets/platform_view.dart:921:36)
<asynchronous suspension>

Code sample

example/lib/main.dart
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: const MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: ElevatedButton(
            onPressed: () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (_) => UiKitView(viewType: 'a_platform_view'),
                ),
              );
            },
            child: Text('Open'),
          ),
        ),
      ),
    );
  }
}

// flutter create a_platform_view --platforms=ios --template=plugin
// edit ios/Classes/APlatformViewPlugin.swift
// edit example/lib/main.dart
// cd example
// flutter run
//
// Tap Open, Hot Restart, tap Open =>
//
// [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: PlatformException(recreating_view, trying to create an already created view, view id: '0', null)
// #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7)
// #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:370:18)
// <asynchronous suspension>
// #2      PlatformViewsService.initUiKitView (package:flutter/src/services/platform_views.dart:270:5)
// <asynchronous suspension>
// #3      _DarwinViewState._createNewUiKitView (package:flutter/src/widgets/platform_view.dart:933:36)
// <asynchronous suspension>
//
//
// 3.24.3: works fine
// 3.27.2: fails
ios/Classes/APlatformViewPlugin.swift
import Flutter
import UIKit

public class APlatformViewPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "a_platform_view", binaryMessenger: registrar.messenger())
    let instance = APlatformViewPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)

    let factory = FLNativeViewFactory()
      registrar.register(factory, withId: "a_platform_view")
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    result(FlutterMethodNotImplemented)
  }
}

class FLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
    func create(
        withFrame frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?
    ) -> FlutterPlatformView {
        return FLNativeView(
            frame: frame,
            viewIdentifier: viewId,
            arguments: args
            )
    }
}

class FLNativeView: NSObject, FlutterPlatformView {
    private var _view: UIView

    init(
        frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?
    ) {
        _view = UIView()
        super.init()
        _view.backgroundColor = UIColor.blue
        let nativeLabel = UILabel()
        nativeLabel.text = "Native text from iOS"
        nativeLabel.textColor = UIColor.white
        nativeLabel.frame = CGRect(x: 0, y: 0, width: 180, height: 48.0)
        _view.addSubview(nativeLabel)
    }

    func view() -> UIView {
        return _view
    }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
example % /usr/local/flutter/flutter/old/3.27.2/bin/flutter run -d iPhone
Resolving dependencies... 
Downloading packages... 
  async 2.11.0 (2.13.0 available)
  boolean_selector 2.1.1 (2.1.2 available)
  characters 1.3.0 (1.4.0 available)
  clock 1.1.1 (1.1.2 available)
> collection 1.19.0 (was 1.18.0) (1.19.1 available)
  fake_async 1.3.1 (1.3.3 available)
  file 7.0.0 (7.0.1 available)
> leak_tracker 10.0.7 (was 10.0.5) (10.0.9 available)
> leak_tracker_flutter_testing 3.0.8 (was 3.0.5) (3.0.9 available)
  lints 5.0.0 (5.1.1 available)
  matcher 0.12.16+1 (0.12.17 available)
  material_color_utilities 0.11.1 (0.12.0 available)
  meta 1.15.0 (1.16.0 available)
  path 1.9.0 (1.9.1 available)
  platform 3.1.5 (3.1.6 available)
  process 5.0.2 (5.0.3 available)
< sky_engine 0.0.0 from sdk flutter (was 0.0.99 from sdk flutter)
  source_span 1.10.0 (1.10.1 available)
> stack_trace 1.12.0 (was 1.11.1) (1.12.1 available)
  stream_channel 2.1.2 (2.1.4 available)
> string_scanner 1.3.0 (was 1.2.0) (1.4.1 available)
  term_glyph 1.2.1 (1.2.2 available)
> test_api 0.7.3 (was 0.7.2) (0.7.4 available)
> vm_service 14.3.0 (was 14.2.5) (15.0.0 available)
> webdriver 3.0.4 (was 3.0.3) (3.1.0 available)
Changed 9 dependencies!
24 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Launching lib/main.dart on iPhone 15 Pro Max iOS17.2 in debug mode...
Warning: Missing build name (CFBundleShortVersionString).
Warning: Missing build number (CFBundleVersion).
Action Required: You must set a build name and number in the pubspec.yaml file version field before submitting to the App Store.
Running pod install...                                             737ms
Running Xcode build...                                                  
 └─Compiling, linking and signing...                      2,335ms
Xcode build done.                                           10.7s
Syncing files to device iPhone 15 Pro Max iOS17.2...                41ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on iPhone 15 Pro Max iOS17.2 is available at: http://127.0.0.1:53020/eX4mEMcChqA=/
The Flutter DevTools debugger and profiler on iPhone 15 Pro Max iOS17.2 is available at:
http://127.0.0.1:9101?uri=http://127.0.0.1:53020/eX4mEMcChqA=/

Performing hot restart...                                               
Restarted application in 306ms.
[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: PlatformException(recreating_view, trying to create an already created view, view id: '0', null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:646:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
<asynchronous suspension>
#2      PlatformViewsService.initUiKitView (package:flutter/src/services/platform_views.dart:248:5)
<asynchronous suspension>
#3      _DarwinViewState._createNewUiKitView (package:flutter/src/widgets/platform_view.dart:921:36)
<asynchronous suspension>
Application finished.

Flutter Doctor output

Doctor output
example % /usr/local/flutter/flutter/old/3.27.2/bin/flutter doctor -v
[!] Flutter (Channel stable, 3.27.2, on macOS 14.7.4 23H420 darwin-arm64, locale en-US)
    • Flutter version 3.27.2 on channel stable at /usr/local/flutter/flutter/old/3.27.2
    ! The flutter binary is not on your path. Consider adding /usr/local/flutter/flutter/old/3.27.2/bin to your path.
    ! The dart binary is not on your path. Consider adding /usr/local/flutter/flutter/old/3.27.2/bin to your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 68415ad1d9 (6 weeks ago), 2025-01-13 10:22:03 -0800
    • Engine revision e672b006cb
    • Dart version 3.6.1
    • DevTools version 2.40.2
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and
      upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/Pau12893/Library/Android/sdk
    • Platform android-35, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio_2023.3.1.18.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
    • Xcode at /Applications/Xcode_16.2.0.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Applications/Android Studio_2023.1.1.27.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] Android Studio (version 2023.3)
    • Android Studio at /Applications/Android Studio_2023.3.1.18.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)

[✓] VS Code (version 1.97.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.104.0

[✓] Connected device (6 available)
    • Pixel 6 (mobile)                   • 26101FDF6005Z5                       • android-arm64  • Android 15 (API 35)
    • Paul’s iPhone (mobile)             • 00008110-0010449E0C45401E            • ios            • iOS 18.3.1 22D72
    • iPhone 15 Pro Max iOS17.2 (mobile) • 21F6081D-57F7-4550-9E59-BDCC87832EA7 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-2
      (simulator)
    • macOS (desktop)                    • macos                                • darwin-arm64   • macOS 14.7.4 23H420 darwin-arm64
    • Mac Designed for iPad (desktop)    • mac-designed-for-ipad                • darwin         • macOS 14.7.4 23H420 darwin-arm64
    • Chrome (web)                       • chrome                               • web-javascript • Google Chrome 133.0.6943.127

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: crashStack traces logged to the consolec: regressionIt was better in the past than it is nowfound in release: 3.29Found to occur in 3.29found in release: 3.30Found to occur in 3.30has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-iosiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-iosOwned by iOS platform team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions