Is it possible to configure firebase database connection settings within the application in flutter? If so, how can I do it?
void main() async{
WidgetsFlutterBinding.ensureInitialized();
final settingsController = Get.put(SettingsController());
await settingsController.loadSettings();`
await Firebase.initializeApp(
options: settingsController.firebaseOptions,
);
runApp(const MyApp());
}
class SettingsController extends GetxController {
var apiKey = ''.obs;
var projectId = ''.obs;
var messagingSenderId = ''.obs;
var appId = ''.obs;
@override
void onInit() {
super.onInit();
loadSettings();
}
Future <void> loadSettings() async {
final prefs = await SharedPreferences.getInstance();
apiKey.value = prefs.getString('apiKey') ?? '';
projectId.value = prefs.getString('projectId') ?? '';
messagingSenderId.value = prefs.getString('messagingSenderId') ?? '';
appId.value = prefs.getString('appId') ?? '';
}
Future <void> saveSettings() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('apiKey', apiKey.value);
await prefs.setString('projectId', projectId.value);
await prefs.setString('messagingSenderId', messagingSenderId.value);
await prefs.setString('appId', appId.value);
}
FirebaseOptions get firebaseOptions => FirebaseOptions(
apiKey: apiKey.value,
projectId: projectId.value,
messagingSenderId: messagingSenderId.value,
appId: appId.value,
);
}
I usually get this error when trying to do this
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [core/duplicate-app] A Firebase App named “[DEFAULT]” already exists
Actually, I had a goal of taking the user’s information and disabling the identification in Firebase Options. Since I do not have any information, I am asking you, if it does not work, I will continue my project with SQLite.
Mutlucan Akgül is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.