Flutter with Firebase Google 登录:断言失败 network_image_io.dart

Posted

技术标签:

【中文标题】Flutter with Firebase Google 登录:断言失败 network_image_io.dart【英文标题】:Flutter with Firebase Google sign in: Failed assertion network_image_io.dart 【发布时间】:2020-07-08 13:17:15 【问题描述】:

我正在尝试使用 Flutter 进行 Firebase 身份验证和 Google 登录,但收到以下错误消息:

'package:flutter/src/painting/_network_image_io.dart': Failed assertion: line 23 pos 14: 'url != null': is not true.

我不明白出了什么问题,你能帮帮我吗?

login.dart 代码 sn-p

-> 从 sign_in.dart 调用 signInWithGoogle() 方法并返回 FirstScreen

Widget _signInButton() 
    return OutlineButton(
      splashColor: Colors.grey,
      onPressed: () 
        signInWithGoogle().whenComplete(() 
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) 
                return FirstScreen();
              ,
            ),
          );
        );
      ,

sign_in.dart 代码 sn-p:

-> 这里我对用户进行身份验证并尝试获取登录的用户名、电子邮件和图像

final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();

String name;
String email;
String imageUrl;

Future<String> signInWithGoogle() async 
  final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
  final GoogleSignInAuthentication googleSignInAuthentication =
  await googleSignInAccount.authentication;

  final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: googleSignInAuthentication.accessToken,
    idToken: googleSignInAuthentication.idToken,
  );

  final AuthResult authResult = await _auth.signInWithCredential(credential);
  final FirebaseUser user = authResult.user;

  // Checking if email and name is null
  assert(user.email != null);
  assert(user.displayName != null);
  assert(user.photoUrl != null);

  name = user.displayName;
  email = user.email;
  imageUrl = user.photoUrl;

  // Only taking the first part of the name, i.e., First Name
  if (name.contains(" ")) 
    name = name.substring(0, name.indexOf(" "));
  

  assert(!user.isAnonymous);
  assert(await user.getIdToken() != null);

  final FirebaseUser currentUser = await _auth.currentUser();
  assert(user.uid == currentUser.uid);

  log('data:  $user');
  return 'signInWithGoogle succeeded: $user';


void signOutGoogle() async 
  await googleSignIn.signOut();

  print("User Sign Out");

FirstScreen 代码 sn-p:

-> 在这里,我尝试根据之前的代码显示用户数据(姓名、电子邮件和图像)

children: <Widget>[
              CircleAvatar(
                backgroundImage: NetworkImage(
                  imageUrl,
                ),
                radius: 60,
                backgroundColor: Colors.transparent,
              ),
              SizedBox(height: 40),
              Text(
                'NAME',
                style: TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.bold,
                    color: Colors.black54),
              ),
              Text(
                name,
                style: TextStyle(
                    fontSize: 25,
                    color: Colors.deepPurple,
                    fontWeight: FontWeight.bold),
              ),
              SizedBox(height: 20),
              Text(
                'EMAIL',
                style: TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.bold,
                    color: Colors.black54),
              ),
              Text(
                email,
                style: TextStyle(
                    fontSize: 25,
                    color: Colors.deepPurple,
                    fontWeight: FontWeight.bold),
              ),
              SizedBox(height: 40)

编辑:附加信息 -> 控制台日志显示“未处理的异常:PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null)”

【问题讨论】:

您是否在 Firebase 控制台中添加了 android debugkey SHA-1 指纹并在添加后更新了 google-services.json 文件? 是的,我做了,但仍然没有工作:( 【参考方案1】:

我发现我遇到的错误是由于缺少一些 OAuth 凭据设置。我找到了解决方案here

【讨论】:

以上是关于Flutter with Firebase Google 登录:断言失败 network_image_io.dart的主要内容,如果未能解决你的问题,请参考以下文章

Flutter with Firebase Google 登录:断言失败 network_image_io.dart

在flutter android应用程序崩溃中添加一些firebase库后

Google Admob 和 Firebase with Analytics 需要哪些权限?

Flutter Login with Facebook 返回构建错误

点击 <goo.gl 按钮后 Firebase DynamicLinks 未打开

如何使用 Cloud Function with Flutter 从新图像中获取 URL?