是否可以在flutter应用程序中配置firebase数据库连接设置?如果可以的话我该怎么办?
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,
);
}
尝试执行此操作时我通常会收到此错误 [错误:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常:[core/duplicate-app] 名为“[DEFAULT]”的 Firebase 应用程序已存在
实际上,我的目标是获取用户的信息并在 Firebase 选项中禁用识别。由于我没有任何信息,所以我问你,如果不起作用,我将继续使用 SQLite 进行我的项目。
class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for web - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return android;
case TargetPlatform.iOS:
return ios;
case TargetPlatform.macOS:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for macos - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.windows:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for windows - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.linux:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for linux - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
default:
throw UnsupportedError(
'DefaultFirebaseOptions are not supported for this platform.',
);
}
}
//----------try with intial value Of firebase Configuration
--------
static const FirebaseOptions android = FirebaseOptions(
apiKey: '',
appId: '',
messagingSenderId: '',
projectId: '',
storageBucket: '',
);
static const FirebaseOptions ios = FirebaseOptions(
apiKey: '',
appId: '',
messagingSenderId: '',
projectId: '',
storageBucket: '',
iosBundleId: '',
);
}
在主.dart
WidgetsFlutterBinding.ensureInitialized();
// Sets the preferred orientation to portrait mode
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
我做了很多次,效果很好