Repository: flutter
Issue: [google_maps_flutter] Marker is flickering when marker update and rebuild.
Labels: platform-android,p: maps,package,has reproducible steps,P2,team-android,triaged-android,found in release: 3.19,found in release: 3.22

### Steps to reproduce

When update the marker List and rebuild, the target marker will flicker. Not every times but easy to reproduce.

Only happened on Android. iOS is good.
Device: Android 14 API 34(whatever simulator or real device)

try this with the minimal code sample.
1. tap the marker  =>  change target marker to yellow
2. tap the map  =>  change yellow marker back to red

it works good before I upgrade SDK version
Before:
> flutter 3.7.12
> google_maps_flutter  2.2.6

After:
> flutter 3.19.6
> google_maps_flutter  2.6.1

Anything helpful? Thanks to everyone.

### Minimal Code sample
<details><summary>main.dart</summary>

```dart
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

void main() {
  runApp(const ProviderScope(
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'GOOGLE MAP FLASH',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Google Map Flash'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[Expanded(child: MapWidget())],
        ),
      ),
    );
  }
}

class MapWidget extends ConsumerStatefulWidget {
  const MapWidget({super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() => _MapWidgetState();
}

class _MapWidgetState extends ConsumerState<MapWidget> {
  final Completer<GoogleMapController> _mapController = Completer<GoogleMapController>();
  late final GoogleMapController controller;
  static const defaultZoom = 16.0;
  late List<Marker> markersList = ref.read(markersProvider);
  late int lastRebuildTime;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      // refresh icon after get markersList from backend
      ref.read(markersProvider.notifier).setState(getMarker());
      controller = await _mapController.future;
    });
  }

  /// a mock of api which returns a list of position
  List<LatLng> getlocation = [
    const LatLng(35.5594, 139.7210),
    const LatLng(35.5604, 139.7210),
    const LatLng(35.5614, 139.7210),
    const LatLng(35.5624, 139.7210),
    const LatLng(35.5634, 139.7210),
    const LatLng(35.5644, 139.7210),
    const LatLng(35.5654, 139.7210),
    const LatLng(35.5664, 139.7210),
    const LatLng(35.5674, 139.7210),
  ];

  getMarker() {
    List<Marker> markerList = [];
    for (var i = 0; i < getlocation.length; i++) {
      var marker = Marker(
        markerId: MarkerId('$i'),
        position: LatLng(getlocation[i].latitude, getlocation[i].longitude),
        icon: BitmapDescriptor.defaultMarker,
        consumeTapEvents: true,
        onTap: () {
          ref.read(nowIndexProvider.notifier).state = i;
        },
        zIndex: 0,
      );
      markerList.add(marker);
    }
    return markerList;
  }

  @override
  Widget build(BuildContext context) {
    lastRebuildTime = DateTime.now().millisecondsSinceEpoch;

    ref.listen(markersProvider, (oldMarker, newMarker) async {
      int rebuildTimeDiff = DateTime.now().millisecondsSinceEpoch - lastRebuildTime;
      if (rebuildTimeDiff < 200) {
        await Future.delayed(Duration(milliseconds: 200 - rebuildTimeDiff));
      }
      setState(() {
        // refresh marker when markersList update
        markersList = newMarker;
      });
    });

    ref.listen(nowIndexProvider, (oldIndex, nowIndex) async {
      if (nowIndex != -1) {
        CameraPosition cameraPos = CameraPosition(target: getlocation[nowIndex], zoom: defaultZoom);
        controller.animateCamera(CameraUpdate.newCameraPosition(cameraPos));
      }

      if (nowIndex == -1) {
        // set all marker to default color
        ref.read(markersProvider.notifier).changeMarker(oldIndex!, BitmapDescriptor.defaultMarker, 0);
      } else if (oldIndex != -1 && oldIndex! < getlocation.length) {
        // change the target marker to special color
        ref.read(markersProvider.notifier).changeMarker(oldIndex, BitmapDescriptor.defaultMarker, 0);
        ref.read(markersProvider.notifier).changeMarker(nowIndex, BitmapDescriptor.defaultMarkerWithHue(60), 1.0);
      } else {
        // change the target marker to special color
        ref.read(markersProvider.notifier).changeMarker(nowIndex, BitmapDescriptor.defaultMarkerWithHue(60), 1.0);
      }
    });

    return GoogleMap(
      mapType: MapType.normal,
      initialCameraPosition: const CameraPosition(target: LatLng(35.5594, 139.7210), zoom: defaultZoom),
      markers: Set.of(markersList),
      onMapCreated: (GoogleMapController controller) {
        if (!_mapController.isCompleted) {
          _mapController.complete(controller);
        }
      },
      mapToolbarEnabled: false,
      zoomControlsEnabled: false,
      myLocationButtonEnabled: false,
      onTap: (argument) async {
        ref.read(nowIndexProvider.notifier).state = -1;
      },
    );
  }
}

class MarkerList extends StateNotifier<List<Marker>> {
  MarkerList() : super([]);

  List<Marker> changeMarker(int index, BitmapDescriptor bitmap, double zIndex) {
    var item = state[index];
    var marker = Marker(
      markerId: item.markerId,
      position: item.position,
      consumeTapEvents: item.consumeTapEvents,
      icon: bitmap,
      onTap: item.onTap,
      zIndex: zIndex,
    );
    state[index] = marker;
    state = [...state];
    return state;
  }

  void setState(List<Marker> newState) {
    state = newState;
  }
}

final markersProvider = StateNotifierProvider<MarkerList, List<Marker>>((ref) {
  return MarkerList();
});

final nowIndexProvider = StateProvider<int>((ref) {
  return -1;
});
```

</details>

<details><summary>pubspec.yaml</summary>

```yaml
name: google_map_flash
description: "A new Flutter project."
version: 1.0.0+1

environment:
  sdk: '>=3.3.1 <4.0.0'
dependencies:
  flutter:
    sdk: flutter
  google_maps_flutter: ^2.6.1
  flutter_riverpod: ^2.5.1

  cupertino_icons: ^1.0.6

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^3.0.0
flutter:
  uses-material-design: true
```

</details>

### Video


https://github.com/flutter/flutter/assets/32993902/8f94e360-b5ca-4d5e-9f56-090de04fad9e



<details><summary>Logs</summary>

```
D/MIUIInput( 7333): [MotionEvent] ViewRootImpl windowName 'com.example.google_map_flash/com.example.google_map_flash.MainActivity', { action=ACTION_DOWN, id[0]=0, pointerCount=1, eventTime=599817703, downTime=599817703, phoneEventTime=17:30:48.953 } moveCount:0
D/MIUIInput( 7333): [MotionEvent] ViewRootImpl windowName 'com.example.google_map_flash/com.example.google_map_flash.MainActivity', { action=ACTION_UP, id[0]=0, pointerCount=1, eventTime=599817745, downTime=599817703, phoneEventTime=17:30:48.995 } moveCount:0
W/ProxyAndroidLoggerBackend( 7333): Too many Flogger logs received before configuration. Dropping old logs.
6
E/FrameEvents( 7333): updateAcquireFence: Did not find frame.
D/MIUIInput( 7333): [MotionEvent] ViewRootImpl windowName 'com.example.google_map_flash/com.example.google_map_flash.MainActivity', { action=ACTION_DOWN, id[0]=0, pointerCount=1, eventTime=599818469, downTime=599818469, phoneEventTime=17:30:49.720 } moveCount:0
D/MIUIInput( 7333): [MotionEvent] ViewRootImpl windowName 'com.example.google_map_flash/com.example.google_map_flash.MainActivity', { action=ACTION_UP, id[0]=0, pointerCount=1, eventTime=599818553, downTime=599818469, phoneEventTime=17:30:49.803 } moveCount:0
W/ProxyAndroidLoggerBackend( 7333): Too many Flogger logs received before configuration. Dropping old logs.
4
E/FrameEvents( 7333): updateAcquireFence: Did not find frame.
D/MIUIInput( 7333): [MotionEvent] ViewRootImpl windowName 'com.example.google_map_flash/com.example.google_map_flash.MainActivity', { action=ACTION_DOWN, id[0]=0, pointerCount=1, eventTime=599819106, downTime=599819106, phoneEventTime=17:30:50.356 } moveCount:0
D/MIUIInput( 7333): [MotionEvent] ViewRootImpl windowName 'com.example.google_map_flash/com.example.google_map_flash.MainActivity', { action=ACTION_UP, id[0]=0, pointerCount=1, eventTime=599819156, downTime=599819106, phoneEventTime=17:30:50.406 } moveCount:0
W/ProxyAndroidLoggerBackend( 7333): Too many Flogger logs received before configuration. Dropping old logs.
42
E/FrameEvents( 7333): updateAcquireFence: Did not find frame.
D/MIUIInput( 7333): [MotionEvent] ViewRootImpl windowName 'com.example.google_map_flash/com.example.google_map_flash.MainActivity', { action=ACTION_DOWN, id[0]=0, pointerCount=1, eventTime=599819817, downTime=599819817, phoneEventTime=17:30:51.067 } moveCount:0
D/MIUIInput( 7333): [MotionEvent] ViewRootImpl windowName 'com.example.google_map_flash/com.example.google_map_flash.MainActivity', { action=ACTION_UP, id[0]=0, pointerCount=1, eventTime=599819885, downTime=599819817, phoneEventTime=17:30:51.135 } moveCount:0
W/ProxyAndroidLoggerBackend( 7333): Too many Flogger logs received before configuration. Dropping old logs.
4
E/FrameEvents( 7333): updateAcquireFence: Did not find frame.
```

</details>

<details><summary>Flutter Doctor output</summary>

```
[✓] Flutter (Channel stable, 3.19.6, on macOS 14.3.1 23D60 darwin-arm64, locale zh-Hans-CN)
    • Flutter version 3.19.6 on channel stable at /Users/a005103/fvm/versions/3.19.6
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 54e66469a9 (5 days ago), 2024-04-17 13:08:03 -0700
    • Engine revision c4cd48e186
    • Dart version 3.3.4
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/a005103/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.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.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.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)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.5)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • 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

[✓] VS Code (version 1.88.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.86.0

[✓] Connected device (5 available)
    • 23127PN0CC (mobile)         • e727544e                             • android-arm64  • Android 14 (API 34)
    • sdk gphone64 arm64 (mobile) • emulator-5554                        • android-arm64  • Android 14 (API 34) (emulator)
    • iPhone 15 (mobile)          • B91C6079-B635-427F-90B6-1E36C2B025CC • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-4 (simulator)
    • macOS (desktop)             • macos                                • darwin-arm64   • macOS 14.3.1 23D60 darwin-arm64
    • Chrome (web)                • chrome                               • web-javascript • Google Chrome 124.0.6367.61

[✓] Network resources
    • All expected network resources are available.
```

</details>