简体   繁体   English

Google 登录错误:输入“未来”<googlesigninaccount?> ' 不是“GoogleSignInAccount”类型的子类型? 在类型转换中</googlesigninaccount?>

[英]Google Sign-In error: type 'Future<GoogleSignInAccount?>' is not a subtype of type 'GoogleSignInAccount?' in type cast

Using FirebaseAuth I'm trying to get google account registration to my app.使用 FirebaseAuth 我正在尝试将 google 帐户注册到我的应用程序。

on my home screen I click on a TextButton to navigate to the ConnectionPage , which on initial use, transfers me to the SignUpWidget , click to sign in with google and it pops the POPUP window allowing me to select my google user, at this point should be Routed to the LoggedInWidget however nothing happens (ie snapshot has no data) and I keep getting the SignUpWidget with error log "I/flutter ( 721): type 'Future<GoogleSignInAccount?>' is not a subtype of type 'GoogleSignInAccount?'在我的主屏幕上,我单击 TextButton 导航到ConnectionPage ,在初始使用时,它会将我转移到SignUpWidget ,单击以使用 google 登录,它会弹出 POPUP window 允许我 select 我的 google 用户,此时应该被路由到LoggedInWidget但是什么也没有发生(即快照没有数据)并且我不断收到带有错误日志“I/flutter(721):类型'Future< GoogleSignInAccount ?>'的SignUpWidget不是'GoogleSignInAccount'类型的子类型?” in type cast"在类型转换中"

There's a casting error somewhere in the GoogleSignInProvider, which im not able to resolve. GoogleSignInProvider 中某处存在转换错误,我无法解决。 any help?有什么帮助吗?

Thank you.谢谢你。

==== Related Dependencies ==== ==== 相关依赖 ====

# Google SignIn
  firebase_auth: ^3.2.0
  google_sign_in: ^5.2.1
# Google Icon
  font_awesome_flutter: ^9.2.0

code snippets代码片段

==== GoogleSignIn Provider ==== ==== Google 登录提供商 ====

//LIBRARIES

//PACKAGES
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';

//PAGES


//utilizing Provider dependency
class GoogleSignInProvider extends ChangeNotifier {
  //class implements the logic of the google sign in when clicking on google sign in button at the SignUpWidget
  final googleSignIn = GoogleSignIn();

  GoogleSignInAccount? _user; // the user that has signed in
  GoogleSignInAccount get user => _user!;

  Future googleLogin() async {
    try {
      final googleUser = googleSignIn
          .signIn(); // user account = account selected in login pop up
      // ignore: unnecessary_null_comparison
      if (googleUser == null) return;
      _user =
          googleUser as GoogleSignInAccount?; // saving account in _user field
      final googleAuth =
          await _user?.authentication; //getting accessToken and IdToken
      final credential = GoogleAuthProvider.credential(
        accessToken: googleAuth!.accessToken,
        idToken: googleAuth.idToken,
      );

      await FirebaseAuth.instance.signInWithCredential(credential); // use credentials to sign into firebaseAuth
    } catch (e) {
      print(e.toString());
    }
    notifyListeners(); // updates the UI
  }

  Future logout() async {
    await googleSignIn.disconnect();
    FirebaseAuth.instance.signOut();
  }
}

==== CONNECTION PAGE ==== ==== 连接页面 ====

//PACKAGES
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

//PAGES
import '../data/vars.dart';
import '../widget/sign_up_widget.dart';
import '../widget/logged_in_widget.dart';

class ConnectionPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: BG_COLOR,
      body: StreamBuilder(
          stream: FirebaseAuth.instance.authStateChanges(),
          builder: (context, snapshot) {
            // case: connection is waiting
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(child: LinearProgressIndicator());
            }
            // case: connection has error
            else if (snapshot.hasError) {
              return Center(
                child: Text(
                  'Something Went Wrong',
                  style: Theme.of(context).textTheme.headline1!.copyWith(),
                ),
              );
            }
            //case: user is logged in already
            else if (snapshot.hasData){
              return LoggedInWidget();
            }
            // case: user is not logged in
            else {
              return SignUpWidget();
                //SignUpWidget();
            }
          }),
    );
  }
}

==== SIGNUPWIDGET ==== ==== 注册小工具 ====

//LIBRARIES

//PACKAGES
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';

//PAGES
import '../data/vars.dart';
import '../provider/google_sign_in.dart';

class SignUpWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final data = MediaQuery.of(context);
    return Scaffold(
      appBar: AppBar(backgroundColor: BG_COLOR,),
      body: Container(
        color: BG_COLOR,
        height: data.size.height,
        width: data.size.width,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            buildSignUp(context),
          ],
        ),
      ),
    );
  }

    Widget buildSignUp(BuildContext context) =>
        ElevatedButton.icon(
          onPressed: () {
            final provider = Provider.of<GoogleSignInProvider>(context,
                listen: false); // implemented in Provider package
            provider
                .googleLogin(); // user provider to call googleLogin method
          },
          style: Theme
              .of(context)
              .elevatedButtonTheme
              .style!
              .copyWith(),
          label: Text('Google Sign In'),
          icon: FaIcon(FontAwesomeIcons.google, color: Colors.white70,),
        );

}

==== LoggedInWidget ==== ==== LoggedInWidget ====

//LIBRARIES
//import 'dart:js';

//PACKAGES

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

//PAGES
import '../data/vars.dart';
import '../provider/google_sign_in.dart';

class LoggedInWidget extends StatelessWidget {
  //final FirebaseAuth firebaseAuth = FirebaseAuth.instance;

  @override
  Widget build(BuildContext context) {
    final data = MediaQuery.of(context);
    final user = FirebaseAuth.instance.currentUser!;
    return Scaffold(
      appBar: AppBar(
        title: Text('Logged In'),
        centerTitle: true,
        actions: [
          TextButton(
              onPressed: () {
                final provider = Provider.of<GoogleSignInProvider>(context, listen:false);
                provider.logout();
              },
              child: Text('Log Out')
          )
        ],
      ),
      body: Container(
        height: data.size.height,
        width: data.size.width,
        padding: EdgeInsets.all(16.0),
        decoration: BoxDecoration(
          color: BG_COLOR,
          border: Border.all(
              width: 3.0, color: Colors.grey, style: BorderStyle.solid),
        ),
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Profile',
              style: Theme.of(context).textTheme.headline1!.copyWith(),
            ),

            //Display Avatar
            SizedBox(
              height: 20,
            ),
            CircleAvatar(
              radius: 50,
              backgroundImage: NetworkImage(user.photoURL!),
            ),

            //Display Name
            SizedBox(
              height: 8,
            ),
            Text(
              'Name: ' + user.displayName!,
              style: Theme.of(context).textTheme.caption!.copyWith(),
            ),

            //Display Email
            SizedBox(
              height: 8,
            ),
            Text(
              'Email: ' + user.email!,
              style: Theme.of(context).textTheme.caption!.copyWith(),
            ),
          ],
        ),
      ),
    );
  }
}

In this line you are missing an await , that's what the error message is about.在这一行中,您缺少await ,这就是错误消息的含义。 Change this:改变这个:

final googleUser = googleSignIn.signIn();

to this:对此:

final googleUser = await googleSignIn.signIn();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “GoogleSignInAccount?”类型的值? 无法分配给“GoogleSignInAccount”类型的变量 - A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount' “GoogleSignInAccount?”类型的值? 无法分配给“GoogleSignInAccount”类型的变量 - A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount' Flutter:“GoogleSignInAccount”类型的值? 不能分配给“GoogleSignInAccount”类型的变量 - Flutter: A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount' flutter firebase googlesign in 说 - “GoogleSignInAccount”类型的值? 不能分配给“GoogleSignInAccount”类型的变量 - flutter firebase googlesign in says - A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount' _CastError(类型&#39;未来<DateTime> &#39; 不是类型转换中类型 &#39;DateTime&#39; 的子类型) - _CastError (type 'Future<DateTime>' is not a subtype of type 'DateTime' in type cast) 键入“未来 <dynamic> &#39;不是&#39;List类型的子类型 <dynamic> &#39;类型转换 - type 'Future<dynamic>' is not a subtype of type 'List<dynamic>' in type cast 输入&#39;未来<List<Appointment> &gt;&#39; 不是 &#39;List 类型的子类型<Appointment> &#39; 在类型转换中 - type 'Future<List<Appointment>>' is not a subtype of type 'List<Appointment>' in type cast _CastError(类型&#39;未来<ByteData?> &#39; 不是类型 &#39;FutureOr 的子类型<ByteData> &#39; 在类型转换中) - _CastError (type 'Future<ByteData?>' is not a subtype of type 'FutureOr<ByteData>' in type cast) 未处理的异常:“字符串”类型不是“未来”类型的子类型<string> ' 在类型转换中</string> - Unhandled Exception: type 'String' is not a subtype of type 'Future<String>' in type cast 输入“未来<null> ' 不是 flutter 和 Firebase 中类型转换中类型 'String' 的子类型</null> - type 'Future<Null>' is not a subtype of type 'String' in type cast in flutter and Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM