diff --git a/packages/cloud_firestore/cloud_firestore/dartpad/lib/main.dart b/packages/cloud_firestore/cloud_firestore/dartpad/lib/main.dart index 03b9d6699aff..82bcf0299d02 100644 --- a/packages/cloud_firestore/cloud_firestore/dartpad/lib/main.dart +++ b/packages/cloud_firestore/cloud_firestore/dartpad/lib/main.dart @@ -36,23 +36,15 @@ enum MovieQuery { extension on Query { /// Create a firebase query from a [MovieQuery] Query queryBy(MovieQuery query) { - switch (query) { - case MovieQuery.fantasy: - return where('genre', arrayContainsAny: ['Fantasy']); - - case MovieQuery.sciFi: - return where('genre', arrayContainsAny: ['Sci-Fi']); - - case MovieQuery.likesAsc: - case MovieQuery.likesDesc: - return orderBy('likes', descending: query == MovieQuery.likesDesc); - - case MovieQuery.year: - return orderBy('year', descending: true); - - case MovieQuery.score: - return orderBy('score', descending: true); - } + return switch (query) { + MovieQuery.fantasy => where('genre', arrayContainsAny: ['Fantasy']), + MovieQuery.sciFi => where('genre', arrayContainsAny: ['Sci-Fi']), + MovieQuery.likesAsc || + MovieQuery.likesDesc => + orderBy('likes', descending: query == MovieQuery.likesDesc), + MovieQuery.year => orderBy('year', descending: true), + MovieQuery.score => orderBy('score', descending: true) + }; } } diff --git a/packages/cloud_firestore/cloud_firestore/example/integration_test/firebase_options.dart b/packages/cloud_firestore/cloud_firestore/example/integration_test/firebase_options.dart index daecca3d327c..07f8169a6b06 100644 --- a/packages/cloud_firestore/cloud_firestore/example/integration_test/firebase_options.dart +++ b/packages/cloud_firestore/cloud_firestore/example/integration_test/firebase_options.dart @@ -23,25 +23,19 @@ class DefaultFirebaseOptions { if (kIsWeb) { return web; } - switch (defaultTargetPlatform) { - case TargetPlatform.android: - return android; - case TargetPlatform.iOS: - return ios; - case TargetPlatform.macOS: - return macos; - case TargetPlatform.windows: - return android; - case TargetPlatform.linux: - throw UnsupportedError( + return switch (defaultTargetPlatform) { + TargetPlatform.android => android, + TargetPlatform.iOS => ios, + TargetPlatform.macOS => macos, + TargetPlatform.windows => android, + TargetPlatform.linux => throw UnsupportedError( 'DefaultFirebaseOptions have not been configured for linux - ' 'you can reconfigure this by running the FlutterFire CLI again.', - ); - default: - throw UnsupportedError( + ), + _ => throw UnsupportedError( 'DefaultFirebaseOptions are not supported for this platform.', - ); - } + ) + }; } static const FirebaseOptions web = FirebaseOptions( diff --git a/packages/cloud_firestore/cloud_firestore/example/integration_test/firebase_options_secondary.dart b/packages/cloud_firestore/cloud_firestore/example/integration_test/firebase_options_secondary.dart index 24e12278e29d..9f2a0c142149 100644 --- a/packages/cloud_firestore/cloud_firestore/example/integration_test/firebase_options_secondary.dart +++ b/packages/cloud_firestore/cloud_firestore/example/integration_test/firebase_options_secondary.dart @@ -23,20 +23,15 @@ class DefaultFirebaseOptions { if (kIsWeb) { return web; } - switch (defaultTargetPlatform) { - case TargetPlatform.android: - return android; - case TargetPlatform.iOS: - return ios; - case TargetPlatform.macOS: - return macos; - case TargetPlatform.windows: - return android; - default: - throw UnsupportedError( + return switch (defaultTargetPlatform) { + TargetPlatform.android => android, + TargetPlatform.iOS => ios, + TargetPlatform.macOS => macos, + TargetPlatform.windows => android, + _ => throw UnsupportedError( 'DefaultFirebaseOptions are not supported for this platform.', - ); - } + ) + }; } static const FirebaseOptions web = FirebaseOptions( diff --git a/packages/cloud_firestore/cloud_firestore/example/lib/firebase_options.dart b/packages/cloud_firestore/cloud_firestore/example/lib/firebase_options.dart index daecca3d327c..07f8169a6b06 100644 --- a/packages/cloud_firestore/cloud_firestore/example/lib/firebase_options.dart +++ b/packages/cloud_firestore/cloud_firestore/example/lib/firebase_options.dart @@ -23,25 +23,19 @@ class DefaultFirebaseOptions { if (kIsWeb) { return web; } - switch (defaultTargetPlatform) { - case TargetPlatform.android: - return android; - case TargetPlatform.iOS: - return ios; - case TargetPlatform.macOS: - return macos; - case TargetPlatform.windows: - return android; - case TargetPlatform.linux: - throw UnsupportedError( + return switch (defaultTargetPlatform) { + TargetPlatform.android => android, + TargetPlatform.iOS => ios, + TargetPlatform.macOS => macos, + TargetPlatform.windows => android, + TargetPlatform.linux => throw UnsupportedError( 'DefaultFirebaseOptions have not been configured for linux - ' 'you can reconfigure this by running the FlutterFire CLI again.', - ); - default: - throw UnsupportedError( + ), + _ => throw UnsupportedError( 'DefaultFirebaseOptions are not supported for this platform.', - ); - } + ) + }; } static const FirebaseOptions web = FirebaseOptions( diff --git a/packages/cloud_firestore/cloud_firestore/example/lib/main.dart b/packages/cloud_firestore/cloud_firestore/example/lib/main.dart index d7f86bf6ccdc..8eab92f7d137 100755 --- a/packages/cloud_firestore/cloud_firestore/example/lib/main.dart +++ b/packages/cloud_firestore/cloud_firestore/example/lib/main.dart @@ -62,23 +62,15 @@ enum MovieQuery { extension on Query { /// Create a firebase query from a [MovieQuery] Query queryBy(MovieQuery query) { - switch (query) { - case MovieQuery.fantasy: - return where('genre', arrayContainsAny: ['fantasy']); - - case MovieQuery.sciFi: - return where('genre', arrayContainsAny: ['sci-fi']); - - case MovieQuery.likesAsc: - case MovieQuery.likesDesc: - return orderBy('likes', descending: query == MovieQuery.likesDesc); - - case MovieQuery.year: - return orderBy('year', descending: true); - - case MovieQuery.rated: - return orderBy('rated', descending: true); - } + return switch (query) { + MovieQuery.fantasy => where('genre', arrayContainsAny: ['fantasy']), + MovieQuery.sciFi => where('genre', arrayContainsAny: ['sci-fi']), + MovieQuery.likesAsc || + MovieQuery.likesDesc => + orderBy('likes', descending: query == MovieQuery.likesDesc), + MovieQuery.year => orderBy('year', descending: true), + MovieQuery.rated => orderBy('rated', descending: true) + }; } } diff --git a/packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/method_channel/utils/source.dart b/packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/method_channel/utils/source.dart index 1512bbabc828..027c74cfd36c 100644 --- a/packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/method_channel/utils/source.dart +++ b/packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/method_channel/utils/source.dart @@ -6,20 +6,16 @@ import '../../pigeon/messages.pigeon.dart'; /// Converts [Source] to [String] String getSourceString(Source source) { - switch (source) { - case Source.server: - return 'server'; - case Source.cache: - return 'cache'; - default: - return 'default'; - } + return switch (source) { + Source.server => 'server', + Source.cache => 'cache', + _ => 'default' + }; } /// Converts [AggregateSource] to [String] String getAggregateSourceString(AggregateSource source) { - switch (source) { - case AggregateSource.server: - return 'server'; - } + return switch (source) { + AggregateSource.server => 'server', + }; } diff --git a/packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/platform_interface/utils/load_bundle_task_state.dart b/packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/platform_interface/utils/load_bundle_task_state.dart index ef803697c092..909b4aec5e82 100644 --- a/packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/platform_interface/utils/load_bundle_task_state.dart +++ b/packages/cloud_firestore/cloud_firestore_platform_interface/lib/src/platform_interface/utils/load_bundle_task_state.dart @@ -6,14 +6,10 @@ import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart'; LoadBundleTaskState convertToTaskState(String state) { - switch (state) { - case 'running': - return LoadBundleTaskState.running; - case 'success': - return LoadBundleTaskState.success; - case 'error': - return LoadBundleTaskState.error; - default: - throw UnsupportedError('Unknown LoadBundleTaskState value: $state.'); - } + return switch (state) { + 'running' => LoadBundleTaskState.running, + 'success' => LoadBundleTaskState.success, + 'error' => LoadBundleTaskState.error, + _ => throw UnsupportedError('Unknown LoadBundleTaskState value: $state.') + }; } diff --git a/packages/cloud_firestore/cloud_firestore_platform_interface/test/pigeon/test_api.dart b/packages/cloud_firestore/cloud_firestore_platform_interface/test/pigeon/test_api.dart index fda97b2fbce8..32d57ebdcb48 100644 --- a/packages/cloud_firestore/cloud_firestore_platform_interface/test/pigeon/test_api.dart +++ b/packages/cloud_firestore/cloud_firestore_platform_interface/test/pigeon/test_api.dart @@ -7,12 +7,12 @@ // ignore_for_file: avoid_relative_lib_imports import 'dart:async'; import 'dart:typed_data' show Uint8List; + +import 'package:cloud_firestore_platform_interface/src/pigeon/messages.pigeon.dart'; import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:cloud_firestore_platform_interface/src/pigeon/messages.pigeon.dart'; - class _TestFirebaseFirestoreHostApiCodec extends StandardMessageCodec { const _TestFirebaseFirestoreHostApiCodec(); @override @@ -63,36 +63,22 @@ class _TestFirebaseFirestoreHostApiCodec extends StandardMessageCodec { @override Object? readValueOfType(int type, ReadBuffer buffer) { - switch (type) { - case 128: - return AggregateQuery.decode(readValue(buffer)!); - case 129: - return AggregateQueryResponse.decode(readValue(buffer)!); - case 130: - return DocumentReferenceRequest.decode(readValue(buffer)!); - case 131: - return FirestorePigeonFirebaseApp.decode(readValue(buffer)!); - case 132: - return PigeonDocumentChange.decode(readValue(buffer)!); - case 133: - return PigeonDocumentOption.decode(readValue(buffer)!); - case 134: - return PigeonDocumentSnapshot.decode(readValue(buffer)!); - case 135: - return PigeonFirebaseSettings.decode(readValue(buffer)!); - case 136: - return PigeonGetOptions.decode(readValue(buffer)!); - case 137: - return PigeonQueryParameters.decode(readValue(buffer)!); - case 138: - return PigeonQuerySnapshot.decode(readValue(buffer)!); - case 139: - return PigeonSnapshotMetadata.decode(readValue(buffer)!); - case 140: - return PigeonTransactionCommand.decode(readValue(buffer)!); - default: - return super.readValueOfType(type, buffer); - } + return switch (type) { + 128 => AggregateQuery.decode(readValue(buffer)!), + 129 => AggregateQueryResponse.decode(readValue(buffer)!), + 130 => DocumentReferenceRequest.decode(readValue(buffer)!), + 131 => FirestorePigeonFirebaseApp.decode(readValue(buffer)!), + 132 => PigeonDocumentChange.decode(readValue(buffer)!), + 133 => PigeonDocumentOption.decode(readValue(buffer)!), + 134 => PigeonDocumentSnapshot.decode(readValue(buffer)!), + 135 => PigeonFirebaseSettings.decode(readValue(buffer)!), + 136 => PigeonGetOptions.decode(readValue(buffer)!), + 137 => PigeonQueryParameters.decode(readValue(buffer)!), + 138 => PigeonQuerySnapshot.decode(readValue(buffer)!), + 139 => PigeonSnapshotMetadata.decode(readValue(buffer)!), + 140 => PigeonTransactionCommand.decode(readValue(buffer)!), + _ => super.readValueOfType(type, buffer) + }; } } diff --git a/packages/cloud_firestore/cloud_firestore_web/lib/src/interop/firestore.dart b/packages/cloud_firestore/cloud_firestore_web/lib/src/interop/firestore.dart index 8b1749502a56..1a03439d45e0 100644 --- a/packages/cloud_firestore/cloud_firestore_web/lib/src/interop/firestore.dart +++ b/packages/cloud_firestore/cloud_firestore_web/lib/src/interop/firestore.dart @@ -50,12 +50,10 @@ Firestore getFirestoreInstance([ } JSString convertListenSource(ListenSource source) { - switch (source) { - case ListenSource.defaultSource: - return 'default'.toJS; - case ListenSource.cache: - return 'cache'.toJS; - } + return switch (source) { + ListenSource.defaultSource => 'default'.toJS, + ListenSource.cache => 'cache'.toJS + }; } /// The Cloud Firestore service interface. @@ -199,17 +197,16 @@ class Firestore extends JsObjectWrapper { firestore_interop.getPersistentCacheIndexManager(jsObject); if (indexManager != null) { - switch (request) { - case PersistenceCacheIndexManagerRequest.enableIndexAutoCreation: - return firestore_interop - .enablePersistentCacheIndexAutoCreation(indexManager); - case PersistenceCacheIndexManagerRequest.disableIndexAutoCreation: - return firestore_interop - .disablePersistentCacheIndexAutoCreation(indexManager); - case PersistenceCacheIndexManagerRequest.deleteAllIndexes: - return firestore_interop - .deleteAllPersistentCacheIndexes(indexManager); - } + return switch (request) { + PersistenceCacheIndexManagerRequest.enableIndexAutoCreation => + firestore_interop + .enablePersistentCacheIndexAutoCreation(indexManager), + PersistenceCacheIndexManagerRequest.disableIndexAutoCreation => + firestore_interop + .disablePersistentCacheIndexAutoCreation(indexManager), + PersistenceCacheIndexManagerRequest.deleteAllIndexes => + firestore_interop.deleteAllPersistentCacheIndexes(indexManager) + }; } else { // ignore: avoid_print print('Firestore: `PersistentCacheIndexManager` is not available'); diff --git a/packages/cloud_firestore/cloud_firestore_web/lib/src/utils/encode_utility.dart b/packages/cloud_firestore/cloud_firestore_web/lib/src/utils/encode_utility.dart index 0b04a36271a5..42257b0f624d 100644 --- a/packages/cloud_firestore/cloud_firestore_web/lib/src/utils/encode_utility.dart +++ b/packages/cloud_firestore/cloud_firestore_web/lib/src/utils/encode_utility.dart @@ -55,79 +55,64 @@ class EncodeUtility { // The [web.FieldPath] class accepts optional args, which cannot be null/empty-string // values. This code below works around that, however limits users to 10 level // deep FieldPaths which the web counterpart supports - switch (length) { - case 1: - return firestore_interop.FieldPath(components[0].toJS); - case 2: - return firestore_interop.FieldPath( - components[0].toJS, components[1].toJS); - case 3: - return firestore_interop.FieldPath( - components[0].toJS, components[1].toJS, components[2].toJS); - case 4: - return firestore_interop.FieldPath(components[0].toJS, - components[1].toJS, components[2].toJS, components[3].toJS); - case 5: - return firestore_interop.FieldPath( - components[0].toJS, - components[1].toJS, - components[2].toJS, - components[3].toJS, - components[4].toJS); - case 6: - return firestore_interop.FieldPath( - components[0].toJS, - components[1].toJS, - components[2].toJS, - components[3].toJS, - components[4].toJS, - components[5].toJS); - case 7: - return firestore_interop.FieldPath( - components[0].toJS, - components[1].toJS, - components[2].toJS, - components[3].toJS, - components[4].toJS, - components[5].toJS, - components[6].toJS); - case 8: - return firestore_interop.FieldPath( - components[0].toJS, - components[1].toJS, - components[2].toJS, - components[3].toJS, - components[4].toJS, - components[5].toJS, - components[6].toJS, - components[7].toJS); - case 9: - return firestore_interop.FieldPath( - components[0].toJS, - components[1].toJS, - components[2].toJS, - components[3].toJS, - components[4].toJS, - components[5].toJS, - components[6].toJS, - components[7].toJS, - components[8].toJS); - case 10: - return firestore_interop.FieldPath( - components[0].toJS, - components[1].toJS, - components[2].toJS, - components[3].toJS, - components[4].toJS, - components[5].toJS, - components[6].toJS, - components[7].toJS, - components[8].toJS, - components[9].toJS); - default: - throw Exception( - 'Firestore web FieldPath only supports 10 levels deep field paths'); - } + return switch (length) { + 1 => firestore_interop.FieldPath(components[0].toJS), + 2 => + firestore_interop.FieldPath(components[0].toJS, components[1].toJS), + 3 => firestore_interop.FieldPath( + components[0].toJS, components[1].toJS, components[2].toJS), + 4 => firestore_interop.FieldPath(components[0].toJS, components[1].toJS, + components[2].toJS, components[3].toJS), + 5 => firestore_interop.FieldPath(components[0].toJS, components[1].toJS, + components[2].toJS, components[3].toJS, components[4].toJS), + 6 => firestore_interop.FieldPath( + components[0].toJS, + components[1].toJS, + components[2].toJS, + components[3].toJS, + components[4].toJS, + components[5].toJS), + 7 => firestore_interop.FieldPath( + components[0].toJS, + components[1].toJS, + components[2].toJS, + components[3].toJS, + components[4].toJS, + components[5].toJS, + components[6].toJS), + 8 => firestore_interop.FieldPath( + components[0].toJS, + components[1].toJS, + components[2].toJS, + components[3].toJS, + components[4].toJS, + components[5].toJS, + components[6].toJS, + components[7].toJS), + 9 => firestore_interop.FieldPath( + components[0].toJS, + components[1].toJS, + components[2].toJS, + components[3].toJS, + components[4].toJS, + components[5].toJS, + components[6].toJS, + components[7].toJS, + components[8].toJS), + 10 => firestore_interop.FieldPath( + components[0].toJS, + components[1].toJS, + components[2].toJS, + components[3].toJS, + components[4].toJS, + components[5].toJS, + components[6].toJS, + components[7].toJS, + components[8].toJS, + components[9].toJS), + _ => throw Exception( + 'Firestore web FieldPath only supports 10 levels deep field paths') + }; } else if (value == FieldPath.documentId) { return firestore_interop.documentId(); } else if (value is Timestamp) { diff --git a/packages/cloud_firestore/cloud_firestore_web/lib/src/utils/web_utils.dart b/packages/cloud_firestore/cloud_firestore_web/lib/src/utils/web_utils.dart index 9c33ed72a0c3..43d3b07cb1a8 100644 --- a/packages/cloud_firestore/cloud_firestore_web/lib/src/utils/web_utils.dart +++ b/packages/cloud_firestore/cloud_firestore_web/lib/src/utils/web_utils.dart @@ -19,14 +19,11 @@ const _kChangeTypeRemoved = 'removed'; String getServerTimestampBehaviorString( ServerTimestampBehavior serverTimestampBehavior, ) { - switch (serverTimestampBehavior) { - case ServerTimestampBehavior.none: - return 'none'; - case ServerTimestampBehavior.estimate: - return 'estimate'; - case ServerTimestampBehavior.previous: - return 'previous'; - } + return switch (serverTimestampBehavior) { + ServerTimestampBehavior.none => 'none', + ServerTimestampBehavior.estimate => 'estimate', + ServerTimestampBehavior.previous => 'previous' + }; } /// Converts a [web.QuerySnapshot] to a [QuerySnapshotPlatform]. @@ -96,16 +93,12 @@ DocumentChangePlatform convertWebDocumentChange( /// Converts a [web.DocumentChange] type into a [DocumentChangeType]. DocumentChangeType convertWebDocumentChangeType(String changeType) { - switch (changeType.toLowerCase()) { - case _kChangeTypeAdded: - return DocumentChangeType.added; - case _kChangeTypeModified: - return DocumentChangeType.modified; - case _kChangeTypeRemoved: - return DocumentChangeType.removed; - default: - throw UnsupportedError('Unknown DocumentChangeType: $changeType.'); - } + return switch (changeType.toLowerCase()) { + _kChangeTypeAdded => DocumentChangeType.added, + _kChangeTypeModified => DocumentChangeType.modified, + _kChangeTypeRemoved => DocumentChangeType.removed, + _ => throw UnsupportedError('Unknown DocumentChangeType: $changeType.') + }; } /// Converts a [web.SnapshotMetadata] to a [SnapshotMetadataPlatform]. @@ -119,19 +112,11 @@ SnapshotMetadataPlatform convertWebSnapshotMetadata( firestore_interop.GetOptions? convertGetOptions(GetOptions? options) { if (options == null) return null; - String? source; - - switch (options.source) { - case Source.serverAndCache: - source = 'default'; - break; - case Source.cache: - source = 'cache'; - break; - case Source.server: - source = 'server'; - break; - } + final source = switch (options.source) { + Source.serverAndCache => 'default', + Source.cache => 'cache', + Source.server => 'server' + }; return firestore_interop.GetOptions(source: source.toJS); }