Firebase在AngularDart项目中抛出错误

时间:2017-09-05 11:17:34

标签: firebase dart firebase-authentication angular-dart

我可以在哪里找到Firebase(.firebaserc)文件AngularDart项目。

Firebase在项目连接时写入以下文件。

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

firebase错误

(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")

它包含初始化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();
  }
}

1 个答案:

答案 0 :(得分:0)

如果您的服务中的代码只应实例化一次,请确保您只提供一次服务。

中提供这样的服务
bootstrap(AppComponent, [...])

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