Repository: flutter
Issue: paint method of the customPainter is called twice when running the application is release mode.
Labels: framework,has reproducible steps,P2,found in release: 3.0,found in release: 3.1,team-framework,triaged-framework

## Steps to Reproduce


1. Run the below code in release mode and check the console or terminal window.
<details>
<summary>Code sample</summary>

```
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('demo'),
      ),
      body: Center(
          child: CustomPaint(
            painter: Painter(),
            child: Container(),
          )),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class Painter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    print('In painter');
    canvas.drawLine(Offset(0, size.height / 2),
        Offset(size.width, size.height / 2), Paint()..color = Colors.red);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
```

</details>

2. As you can see in the above code, I have written a print statement in the start of paint method. When the application is ran in release mode, the paint method gets called twice. Appended the console screenshot below for refernce.
![image](https://user-images.githubusercontent.com/63577117/171838938-747b1334-4f67-4490-85fe-61fa19d2d180.png)

3. But, when running the same application in debug mode with breakpoints, the paint method hits only once.
![image](https://user-images.githubusercontent.com/63577117/171839092-81785b14-94a8-4342-b147-170034a28fa3.png)

Kindly please check on this and let me know how to overcome this issue else correct me if am doing anything wrong.

<details>
  <summary>Logs</summary>

```
[√] Flutter (Channel stable, 3.0.1, on Microsoft Windows [Version 10.0.19043.1706], locale en-US)
    • Flutter version 3.0.1 at C:\src\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision fb57da5f94 (2 weeks ago), 2022-05-19 15:50:29 -0700
    • Engine revision caaafc5604
    • Dart version 2.17.1
    • DevTools version 2.12.2

[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0-rc2)
    • Android SDK at C:\Users\SriramKiranSenthilku\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0-rc2
    • Java binary at: C:\Program Files\Android\Android Studio1\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.2.0 Preview 2.1)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Preview
    • Visual Studio Community 2022 version 17.2.32317.152
    • The current Visual Studio installation is a pre-release version. It may not be supported by Flutter yet.
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio1
    • 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 11.0.10+0-b96-7249189)

[√] VS Code, 64-bit edition (version 1.67.2)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.40.0

[√] Connected device (4 available)
    • ASUS Z017DB (mobile) • G8AZCY0137034JK • android-arm64  • Android 8.0.0 (API 26)
    • Windows (desktop)    • windows         • windows-x64    • Microsoft Windows [Version 10.0.19043.1706]
    • Chrome (web)         • chrome          • web-javascript • Google Chrome 101.0.4951.67
    • Edge (web)           • edge            • web-javascript • Microsoft Edge 99.0.1150.39

[√] HTTP Host Availability
    • All required HTTP hosts are available
```

</details>