首页 > 解决方案 > Flutter firebase静默登录不起作用

问题描述

我正在使用firebase auth开发一个带有flutter的webapp。我需要一个静默登录功能。这就是我尝试过的:我在 main.dart 中的构建方法:

       return user != null
    ? FutureBuilder(
        future: widget._userRepo.getUserById(user.uid),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            if (user != null) {
              BlocProvider.of<SignInCubit>(context)
                  .setInitialUserData(snapshot.data);
            }
            return MaterialApp(
                title: 'Name',
                theme: ThemeData(
                    highlightColor: CustomColours.darkBlue,
                    primarySwatch: Colors.blue,
                    textTheme: Theme.of(context).textTheme.apply(
                        bodyColor: Colors.white,
                        fontFamily: 'FuturaLight')),
                home: width > height
                    ? /*CodePage(_gameRepo, _heroRepo, _tournamentRepo, _userRepo,
                        "123456", _teamRepo)*/
                    LandingPage(
                        widget._tournamentRepo,
                        widget._userRepo,
                        widget._gameRepo,
                        widget._heroRepo,
                        widget._teamRepo)
                    : NoMobilePage());
          }
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Container();
          } else {
            return Container();
          }
        })
    : MaterialApp(
        title: 'Name',
        theme: ThemeData(
            highlightColor: CustomColours.darkBlue,
            primarySwatch: Colors.blue,
            textTheme: Theme.of(context)
                .textTheme
                .apply(bodyColor: Colors.white, fontFamily: 'FuturaLight')),
        home: width > height
            ? /*CodePage(_gameRepo, _heroRepo, _tournamentRepo, _userRepo,
                        "123456", _teamRepo)*/
            LandingPage(widget._tournamentRepo, widget._userRepo,
                widget._gameRepo, widget._heroRepo, widget._teamRepo)
            : NoMobilePage());

hte userRepo 中的方法:

  Future<CustomUser> getUserById(String id) async {
final Map<String, dynamic> userData =
    await _userDataService.getUserById(id);
if (userData == null) {
  return null;
} else {
  return CustomUser.fromJson(userData);
}
}

用户服务:

  Future<Map<String, dynamic>> getUserById(String id) async {
if (id == null) {
  return null;
}
DocumentSnapshot document =
    await _firebaseFirestore.collection(_collectionPath).doc(id).get();

return document.data();

我从这里获取用户,在 main.dart 中:

 class _MyAppState extends State<MyApp> {
  Future<User> user;
  @override
  void initState() {
    user = widget._googleSignInRepo.getUser();

    super.initState();
  }

这是 getUser 函数:

 User getUser() {
return _googleAuthService.getUser();

}

但是,当我运行它时,用户变量是一个错误:“<getObject: NoSuchMethodError: The getter 'uri' was called on null.>”。我做错了什么?谢谢

标签: firebaseflutterfirebase-authenticationgoogle-signinflutter-web

解决方案


推荐阅读