Selaa lähdekoodia

Add lint rule to warn for unawaited futures

Neeraj Gupta 2 vuotta sitten
vanhempi
commit
943c5edfb6

+ 3 - 0
analysis_options.yaml

@@ -32,6 +32,7 @@ linter:
     - directives_ordering
     - always_use_package_imports
     - sort_child_properties_last
+    - unawaited_futures
 
 analyzer:
   errors:
@@ -59,6 +60,8 @@ analyzer:
     unnecessary_const: error
     cancel_subscriptions: error
 
+
+    unawaited_futures: info # convert to warning after fixing existing issues
     invalid_dependency: info
     use_build_context_synchronously: ignore # experimental lint, requires many changes
     prefer_interpolation_to_compose_strings: ignore # later too many warnings

+ 2 - 2
lib/core/configuration.dart

@@ -537,7 +537,7 @@ class Configuration {
   Future<void> setBackupOverMobileData(bool value) async {
     await _preferences.setBool(keyShouldBackupOverMobileData, value);
     if (value) {
-      SyncService.instance.sync();
+      SyncService.instance.sync().ignore();
     }
   }
 
@@ -562,7 +562,7 @@ class Configuration {
   Future<void> setShouldBackupVideos(bool value) async {
     await _preferences.setBool(keyShouldBackupVideos, value);
     if (value) {
-      SyncService.instance.sync();
+      SyncService.instance.sync().ignore();
     } else {
       SyncService.instance.onVideoBackupPaused();
     }

+ 3 - 1
lib/main.dart

@@ -188,7 +188,9 @@ Future _runWithLogs(Function() function, {String prefix = ""}) async {
 }
 
 Future<void> _scheduleHeartBeat(
-    SharedPreferences prefs, bool isBackground) async {
+  SharedPreferences prefs,
+  bool isBackground,
+) async {
   await prefs.setInt(
     isBackground ? kLastBGTaskHeartBeatTime : kLastFGTaskHeartBeatTime,
     DateTime.now().microsecondsSinceEpoch,

+ 2 - 2
lib/services/user_remote_flag_service.dart

@@ -30,13 +30,13 @@ class UserRemoteFlagService {
   bool shouldShowRecoveryVerification() {
     if (!_prefs.containsKey(needRecoveryKeyVerification)) {
       // fetch the status from remote
-      unawaited(_refreshRecoveryVerificationFlag());
+      _refreshRecoveryVerificationFlag().ignore();
       return false;
     } else {
       final bool shouldShow = _prefs.getBool(needRecoveryKeyVerification)!;
       if (shouldShow) {
         // refresh the status to check if user marked it as done on another device
-        unawaited(_refreshRecoveryVerificationFlag());
+        _refreshRecoveryVerificationFlag().ignore();
       }
       return shouldShow;
     }

+ 1 - 1
lib/services/user_service.dart

@@ -531,7 +531,7 @@ class UserService {
       );
     } catch (e) {
       await dialog.hide();
-      showErrorDialog(
+      await showErrorDialog(
         context,
         "Incorrect recovery key",
         "The recovery key you entered is incorrect",

+ 10 - 6
lib/ui/account/password_reentry_page.dart

@@ -1,5 +1,7 @@
 // @dart=2.9
 
+import 'dart:async';
+
 import 'package:flutter/material.dart';
 import 'package:logging/logging.dart';
 import 'package:photos/core/configuration.dart';
@@ -128,13 +130,15 @@ class _PasswordReentryPageState extends State<PasswordReentryPage> {
           }
           await dialog.hide();
           Bus.instance.fire(SubscriptionPurchasedEvent());
-          Navigator.of(context).pushAndRemoveUntil(
-            MaterialPageRoute(
-              builder: (BuildContext context) {
-                return const HomeWidget();
-              },
+          unawaited(
+            Navigator.of(context).pushAndRemoveUntil(
+              MaterialPageRoute(
+                builder: (BuildContext context) {
+                  return const HomeWidget();
+                },
+              ),
+              (route) => false,
             ),
-            (route) => false,
           );
         },
       ),

+ 1 - 1
lib/ui/collections/trash_button_widget.dart

@@ -116,7 +116,7 @@ class _TrashButtonWidgetState extends State<TrashButtonWidget> {
           ),
         ),
       ),
-      onPressed: () async {
+      onPressed: () {
         routeToPage(
           context,
           TrashPage(),

+ 8 - 4
lib/ui/home/preserve_footer_widget.dart

@@ -1,3 +1,5 @@
+import 'dart:async';
+
 import 'package:flutter/material.dart';
 import 'package:photo_manager/photo_manager.dart';
 import 'package:photos/services/local_sync_service.dart';
@@ -17,10 +19,12 @@ class PreserveFooterWidget extends StatelessWidget {
           if (LocalSyncService.instance.hasGrantedLimitedPermissions()) {
             await PhotoManager.presentLimited();
           } else {
-            routeToPage(
-              context,
-              const BackupFolderSelectionPage(
-                buttonText: "Preserve",
+            unawaited(
+              routeToPage(
+                context,
+                const BackupFolderSelectionPage(
+                  buttonText: "Preserve",
+                ),
               ),
             );
           }

+ 1 - 1
lib/ui/settings/about_section_widget.dart

@@ -117,7 +117,7 @@ class AboutMenuItemWidget extends StatelessWidget {
       pressedColor: getEnteColorScheme(context).fillFaint,
       trailingIcon: Icons.chevron_right_outlined,
       trailingIconIsMuted: true,
-      onTap: () async {
+      onTap: () {
         Navigator.of(context).push(
           MaterialPageRoute(
             builder: (BuildContext context) {

+ 12 - 8
lib/ui/settings/account_section_widget.dart

@@ -1,5 +1,7 @@
 // @dart=2.9
 
+import 'dart:async';
+
 import 'package:flutter/material.dart';
 import 'package:flutter_sodium/flutter_sodium.dart';
 import 'package:photos/services/local_authentication_service.dart';
@@ -49,16 +51,18 @@ class AccountSectionWidget extends StatelessWidget {
               try {
                 recoveryKey = await _getOrCreateRecoveryKey(context);
               } catch (e) {
-                showGenericErrorDialog(context);
+                await showGenericErrorDialog(context);
                 return;
               }
-              routeToPage(
-                context,
-                RecoveryKeyPage(
-                  recoveryKey,
-                  "OK",
-                  showAppBar: true,
-                  onDone: () {},
+              unawaited(
+                routeToPage(
+                  context,
+                  RecoveryKeyPage(
+                    recoveryKey,
+                    "OK",
+                    showAppBar: true,
+                    onDone: () {},
+                  ),
                 ),
               );
             }

+ 1 - 1
lib/ui/settings/danger_section_widget.dart

@@ -91,7 +91,7 @@ class DangerSectionWidget extends StatelessWidget {
       ],
     );
 
-    showDialog(
+    await showDialog(
       context: context,
       builder: (BuildContext context) {
         return alert;

+ 1 - 1
lib/ui/settings/security_section_widget.dart

@@ -265,7 +265,7 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
         ],
       );
 
-      showDialog(
+      await showDialog(
         context: context,
         builder: (BuildContext context) {
           return alert;

+ 2 - 1
lib/utils/thumbnail_util.dart

@@ -162,7 +162,8 @@ Future<void> _downloadAndDecryptThumbnail(FileDownloadItem item) async {
   if (cachedThumbnail.existsSync()) {
     await cachedThumbnail.delete();
   }
-  cachedThumbnail.writeAsBytes(data);
+  // data is already cached in-memory, no need to await on dist write
+  unawaited(cachedThumbnail.writeAsBytes(data));
   if (_map.containsKey(file.uploadedFileID)) {
     try {
       item.completer.complete(data);