initializeFromConfig static method
Initializes Firebase using configuration from SFConfig.
This method retrieves Firebase configuration variables from SFConfig and initializes Firebase accordingly.
Parameters:
isDev
: Iftrue
, Firebase Auth will use the local emulator on localhost:9099. Defaults tofalse
. Ifnull
, the mode will be determined automatically based on SFConfig and kDebugMode.
Throws:
- Exception if any required Firebase configuration variable is missing.
Implementation
static Future<void> initializeFromConfig({bool? isDev}) async {
final apiKey = SFConfig.get<String>('FIREBASE_API_KEY');
final authDomain = SFConfig.get<String>('FIREBASE_AUTH_DOMAIN');
final projectId = SFConfig.get<String>('FIREBASE_PROJECT_ID');
final storageBucket = SFConfig.get<String>('FIREBASE_STORAGE_BUCKET');
final messagingSenderId = SFConfig.get<String>(
'FIREBASE_MESSAGING_SENDER_ID',
);
final appId = SFConfig.get<String>('FIREBASE_APP_ID');
// Verify that all required variables are present
final requiredFields = {
'FIREBASE_API_KEY': apiKey,
'FIREBASE_AUTH_DOMAIN': authDomain,
'FIREBASE_PROJECT_ID': projectId,
'FIREBASE_STORAGE_BUCKET': storageBucket,
'FIREBASE_MESSAGING_SENDER_ID': messagingSenderId,
'FIREBASE_APP_ID': appId,
};
final missingFields =
requiredFields.entries
.where((entry) => entry.value == null)
.map((entry) => entry.key)
.toList();
if (missingFields.isNotEmpty) {
throw Exception(
'Missing Firebase configuration variables: ${missingFields.join(', ')}',
);
}
// Automatically determine if we are in development mode
final useDevMode = isDev ?? (SFConfig.isDevelopment || kDebugMode);
// Use the existing initialization method
await initialize(
apiKey: apiKey!,
authDomain: authDomain!,
projectId: projectId!,
storageBucket: storageBucket!,
messagingSenderId: messagingSenderId!,
appId: appId!,
isDev: useDevMode,
);
}