简体   繁体   中英

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();
  }
}

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 {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM