Skip to content

Commit c7963d6

Browse files
authored
feat(firebase_core_platform_interface): Add copyWith to FirebaseOptions (#13084)
* Add copyWith to FirebaseOptions * Add test
1 parent 806c15d commit c7963d6

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

packages/firebase_core/firebase_core_platform_interface/lib/src/firebase_options.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,42 @@ class FirebaseOptions {
7373
iosBundleId = options.iosBundleId,
7474
appGroupId = options.appGroupId;
7575

76+
/// Returns a copy of this FirebaseOptions with the given fields replaced with
77+
/// the new values.
78+
FirebaseOptions copyWith({
79+
String? apiKey,
80+
String? appId,
81+
String? messagingSenderId,
82+
String? projectId,
83+
String? authDomain,
84+
String? databaseURL,
85+
String? storageBucket,
86+
String? measurementId,
87+
String? trackingId,
88+
String? deepLinkURLScheme,
89+
String? androidClientId,
90+
String? iosClientId,
91+
String? iosBundleId,
92+
String? appGroupId,
93+
}) {
94+
return FirebaseOptions(
95+
apiKey: apiKey ?? this.apiKey,
96+
appId: appId ?? this.appId,
97+
messagingSenderId: messagingSenderId ?? this.messagingSenderId,
98+
projectId: projectId ?? this.projectId,
99+
authDomain: authDomain ?? this.authDomain,
100+
databaseURL: databaseURL ?? this.databaseURL,
101+
storageBucket: storageBucket ?? this.storageBucket,
102+
measurementId: measurementId ?? this.measurementId,
103+
trackingId: trackingId ?? this.trackingId,
104+
deepLinkURLScheme: deepLinkURLScheme ?? this.deepLinkURLScheme,
105+
androidClientId: androidClientId ?? this.androidClientId,
106+
iosClientId: iosClientId ?? this.iosClientId,
107+
iosBundleId: iosBundleId ?? this.iosBundleId,
108+
appGroupId: appGroupId ?? this.appGroupId,
109+
);
110+
}
111+
76112
/// An API key used for authenticating requests from your app to Google
77113
/// servers.
78114
final String apiKey;

packages/firebase_core/firebase_core_platform_interface/test/firebase_options_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,52 @@ void main() {
6767
expect(options1 == options2, isTrue);
6868
});
6969

70+
test('should copyWith new values', () {
71+
const options = FirebaseOptions(
72+
apiKey: 'apiKey',
73+
appId: 'appId',
74+
messagingSenderId: 'messagingSenderId',
75+
projectId: 'projectId',
76+
);
77+
78+
final newOptions = options.copyWith(
79+
apiKey: 'newApiKey',
80+
appId: 'newAppId',
81+
messagingSenderId: 'newMessagingSenderId',
82+
projectId: 'newProjectId',
83+
authDomain: 'newAuthDomain',
84+
databaseURL: 'newDatabaseURL',
85+
storageBucket: 'newStorageBucket',
86+
measurementId: 'newMeasurementId',
87+
trackingId: 'newTrackingId',
88+
deepLinkURLScheme: 'newDeepLinkURLScheme',
89+
androidClientId: 'newAndroidClientId',
90+
iosClientId: 'newIosClientId',
91+
iosBundleId: 'newIosBundleId',
92+
appGroupId: 'newAppGroupId',
93+
);
94+
95+
expect(
96+
newOptions,
97+
const FirebaseOptions(
98+
apiKey: 'newApiKey',
99+
appId: 'newAppId',
100+
messagingSenderId: 'newMessagingSenderId',
101+
projectId: 'newProjectId',
102+
authDomain: 'newAuthDomain',
103+
databaseURL: 'newDatabaseURL',
104+
storageBucket: 'newStorageBucket',
105+
measurementId: 'newMeasurementId',
106+
trackingId: 'newTrackingId',
107+
deepLinkURLScheme: 'newDeepLinkURLScheme',
108+
androidClientId: 'newAndroidClientId',
109+
iosClientId: 'newIosClientId',
110+
iosBundleId: 'newIosBundleId',
111+
appGroupId: 'newAppGroupId',
112+
),
113+
);
114+
});
115+
70116
test('should return a Map', () {
71117
const options = FirebaseOptions(
72118
apiKey: 'apiKey',

0 commit comments

Comments
 (0)