Repository: flutter
Issue: Hero widget's child triggers initState repeatedly during route animation without a key
Labels: framework,a: animation,f: routes,P2,team-framework,triaged-framework

### Steps to reproduce

Hi Flutter team,

I've encountered an issue where the child of a `Hero` widget triggers `initState` repeatedly during the route animation if no key is set. Here are the details:

Steps to Reproduce:

Create a `Hero` widget with a child widget that has an `initState` method.
Do not set a key for the child widget.
Navigate to a new route with a Hero animation.
Observe that the initState method of the child widget is triggered multiple times during the animation.

### Expected results

The `initState` method should only be called once when the child widget is first created.



### Actual results

The `initState` method is called repeatedly during the route animation, causing unexpected behavior.

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() => runApp(const HeroApp());

class HeroApp extends StatelessWidget {
  const HeroApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Transition Demo',
      home: MainScreen(),
    );
  }
}

class MainScreen extends StatelessWidget {
  const MainScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Main Screen'),
      ),
      body: GestureDetector(
        onTap: () {
          Navigator.push(context, MaterialPageRoute(builder: (context) {
            return DetailScreen();
          }));
        },
        child: Hero(
          tag: 'imageHero',
          child: TestWidget(
            tag: 'MainScreen',
          ),
        ),
      ),
    );
  }
}

class DetailScreen extends StatelessWidget {
  const DetailScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        onTap: () {
          Navigator.pop(context);
        },
        child: Center(
          child: Hero(
            tag: 'imageHero',
            child: TestWidget(
              tag: 'DetailScreen',
            ),
          ),
        ),
      ),
    );
  }
}

class TestWidget extends StatefulWidget {
  const TestWidget({super.key, required this.tag});

  final String tag;

  @override
  State<TestWidget> createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {
  @override
  void initState() {
    super.initState();
    print('initState ${widget.tag} ${this.hashCode}');
  }

  @override
  Widget build(BuildContext context) {
    return Image.network(
      'https://picsum.photos/250?image=9',
    );
  }
}

```

</details>

### Logs

<details open><summary>Logs</summary>

```console
Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
Running Gradle task 'assembleDebug'...
✓ Built build/app/outputs/flutter-apk/app-debug.apk
Installing build/app/outputs/flutter-apk/app-debug.apk...
Debug service listening on ws://127.0.0.1:53582/nkyWNoVtFJ0=/ws
Syncing files to device sdk gphone64 arm64...
I/flutter ( 3001): initState MainScreen 649717867
D/EGL_emulation( 3001): app_time_stats: avg=1109.82ms min=21.97ms max=2197.66ms count=2
I/flutter ( 3001): initState DetailScreen 743079764
W/WindowOnBackDispatcher( 3001): OnBackInvokedCallback is not enabled for the application.
W/WindowOnBackDispatcher( 3001): Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
D/EGL_emulation( 3001): app_time_stats: avg=4202.80ms min=4202.80ms max=4202.80ms count=1
I/flutter ( 3001): initState DetailScreen 458387907
I/flutter ( 3001): initState DetailScreen 505844363

```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[!] Flutter (Channel stable, 3.22.2, on macOS 13.5.1 22G90 darwin-arm64, locale zh-Hans-CN)
    • Flutter version 3.22.2 on channel stable at /Users/cd/fvm/versions/stable
    ! The flutter binary is not on your path. Consider adding /Users/cd/fvm/versions/stable/bin to your path.
    ! The dart binary is not on your path. Consider adding /Users/cd/fvm/versions/stable/bin to your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 761747bfc5 (8 days ago), 2024-06-05 22:15:13 +0200
    • Engine revision edd8546116
    • Dart version 3.4.3
    • DevTools version 2.34.3
    • 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/cd/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.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 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    ! Flutter recommends a minimum Xcode version of 15.
      Download the latest version or update via the Mac App Store.
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.3)
    • Android Studio at /Applications/Android Studio.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.90.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.90.0

[✓] Connected device (4 available)
    • sdk gphone64 arm64 (mobile)     • emulator-5554         • android-arm64  • Android 14 (API 34) (emulator)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 13.5.1 22G90 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 13.5.1 22G90 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 126.0.6478.61

[✓] Network resources
    • All expected network resources are available.
```

</details>