Tushar Rai Tushar Rai - 29 days ago 7
Dart Question

Firebase throws error in an AngularDart project

Where I can find the Firebase (.firebaserc) file an AngularDart project.

Firebase writes the following file while project connection.

Writing configuration info to firebase.json...
Writing project information to .firebaserc...


firebase error

(anonymous function) Uncaught Unhandled exception:
EXCEPTION: Error in
package:my_app/views/app_component/app_component.html:0:0
ORIGINAL EXCEPTION: NoSuchMethodError: Class 'String' has no instance method
'_hasProperty'.
Receiver: "[DEFAULT]: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app)."
Tried calling: _hasProperty("message")


It contains the code which initializes the firebase.
firebase_service.dart

import 'dart:async';
import 'package:angular/angular.dart';
import 'package:firebase/firebase.dart' as fb;

@Injectable()
class FirebaseService {
fb.User user;
fb.Auth _fbAuth;
fb.GoogleAuthProvider _fbGoogleAuthProvider;

FirebaseService() {
fb.initializeApp(
apiKey: "AIzaSyBOShlCgUeqTL99n32bjWdNlkH1111111",
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app.firebaseio.com",
storageBucket: "my-app.appspot.com",
);

_fbGoogleAuthProvider = new fb.GoogleAuthProvider();
_fbAuth = fb.auth();
_fbAuth.onAuthStateChanged.listen(_authChanged);
}

void _authChanged(fb.User event) {
user = event;
}

Future signIn() async {
try {
await _fbAuth.signInWithPopup(_fbGoogleAuthProvider);
}
catch (error) {
print("$runtimeType::login() -- $error");
}
}

void signOut() {
_fbAuth.signOut();
}
}

Answer Source

If you have code in a service that should only be instantiated once, ensure that you only provide the service exactly once.

Provide such a service either only in

bootstrap(AppComponent, [...])

or alternatively only in

@Component(
  ..., 
  providers: const [...])
}
class AppComponent {}