Ensure analytics are sent before disabling (#62094)

diff --git a/packages/flutter_tools/lib/src/commands/config.dart b/packages/flutter_tools/lib/src/commands/config.dart
index 639454f..ecfd5ba 100644
--- a/packages/flutter_tools/lib/src/commands/config.dart
+++ b/packages/flutter_tools/lib/src/commands/config.dart
@@ -113,9 +113,15 @@
 
     if (argResults.wasParsed('analytics')) {
       final bool value = boolArg('analytics');
-      // We send the analytics event *before* toggling the flag intentionally
-      // to be sure that opt-out events are sent correctly.
+      // The tool sends the analytics event *before* toggling the flag
+      // intentionally to be sure that opt-out events are sent correctly.
       AnalyticsConfigEvent(enabled: value).send();
+      if (!value) {
+        // Normally, the tool waits for the analytics to all send before the
+        // tool exits, but only when analytics are enabled. When reporting that
+        // analytics have been disable, the wait must be done here instead.
+        await globals.flutterUsage.ensureAnalyticsSent();
+      }
       globals.flutterUsage.enabled = value;
       globals.printStatus('Analytics reporting ${value ? 'enabled' : 'disabled'}.');
     }
diff --git a/packages/flutter_tools/test/commands.shard/hermetic/config_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/config_test.dart
index 54b2604..ee5a2a5 100644
--- a/packages/flutter_tools/test/commands.shard/hermetic/config_test.dart
+++ b/packages/flutter_tools/test/commands.shard/hermetic/config_test.dart
@@ -233,6 +233,9 @@
 
       expect(mockUsage.enabled, false);
 
+      // Verify that we flushed the analytics queue.
+      verify(mockUsage.ensureAnalyticsSent());
+
       // Verify that we only send the analytics disable event, and no other
       // info.
       verifyNever(mockUsage.sendCommand(