sudama221
@sudama221

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

flutterでfirebaseを使用したgoogleログインができない

いろいろな記事を参考にして、googleログインを実装したいのですが、できません。

コードやfirebaseなどでの行動はこちらと同じことをしています。

main.dart

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

void main() => runApp(MyHomePage());

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GoogleSignIn _googleSignIn = GoogleSignIn();
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<FirebaseUser> _handleSignIn() async {
    GoogleSignInAccount googleCurrentUser = _googleSignIn.currentUser;
    try {
      if (googleCurrentUser == null)
        googleCurrentUser = await _googleSignIn.signInSilently();
      if (googleCurrentUser == null)
        googleCurrentUser = await _googleSignIn.signIn();
      if (googleCurrentUser == null) return null;

      GoogleSignInAuthentication googleAuth =
          await googleCurrentUser.authentication;
      final AuthCredential credential = GoogleAuthProvider.getCredential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );
      final FirebaseUser user =
          (await _auth.signInWithCredential(credential)).user;
      print("signed in " + user.displayName);

      return user;
    } catch (e) {
      print(e);
      return null;
    }
  }

  void transitionNextPage(FirebaseUser user) {
    if (user == null) return;

    Navigator.push(context,
        MaterialPageRoute(builder: (context) => NextPage(userData: user)));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                child: Text('Sign in Google'),
                onPressed: () {
                  _handleSignIn()
                      .then((FirebaseUser user) => transitionNextPage(user))
                      .catchError((e) => print(e));
                },
              ),
            ]),
      ),
    );
  }
}

// ignore: must_be_immutable
class NextPage extends StatefulWidget {
  FirebaseUser userData;

  NextPage({Key key, this.userData}) : super(key: key);

  @override
  _NextPageState createState() => _NextPageState(userData);
}

class _NextPageState extends State<NextPage> {
  FirebaseUser userData;
  String name = "";
  String email;
  String photoUrl;
  final GoogleSignIn _googleSignIn = GoogleSignIn();

  _NextPageState(FirebaseUser userData) {
    this.userData = userData;
    this.name = userData.displayName;
    this.email = userData.email;
    this.photoUrl = userData.photoUrl;
  }

  Future<void> _handleSignOut() async {
    await FirebaseAuth.instance.signOut();
    try {
      await _googleSignIn.signOut();
    } catch (e) {
      print(e);
    }
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("ユーザー情報表示"),
      ),
      body: Center(
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Image.network(this.photoUrl),
              Text(
                this.name,
                style: TextStyle(
                  fontSize: 24,
                ),
              ),
              Text(
                this.email,
                style: TextStyle(
                  fontSize: 24,
                ),
              ),
              RaisedButton(
                child: Text('Sign Out Google'),
                onPressed: () {
                  _handleSignOut().catchError((e) => print(e));
                },
              ),
            ]),
      ),
    );
  }
}

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^0.4.2+1
  firebase_auth: ^0.15.1
  google_sign_in: ^4.0.14

出ているエラー内容
・vscode内デバックコンソール

Configuring the default Firebase app...
Configured the default Firebase app __FIRAPP_DEFAULT.
Connecting to VM Service at ws://127.0.0.1:56575/Su7urt4oDr0=/ws

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building MyHomePage(dirty, state: _MyHomePageState#be78c):
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'


The relevant error-causing widget was
MyHomePage
package:ota/main.dart:5
When the exception was thrown, this was the stack
#2      new Text
package:flutter/…/widgets/text.dart:370
#3      _MyHomePageState.build
package:ota/main.dart:57
#4      StatefulElement.build
package:flutter/…/widgets/framework.dart:4744
#5      ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4627
#6      StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4800
...

ios シミュレーター内のエラー

スクリーンショット 2021-01-05 17.53.21.png

記述が足りないところがありましたら、教えてください。

0

1Answer

エラーの内容を見る限り、 Text に必須で渡すべき文字列が null である、ということのようですね。

全ては確認できていませんが、おそらく 57 行目の widget.title が null になっているのではないかと思います。 widget.title がどこから来ているのか、何の値が入っているのか(もしくは入っていないのか)を追ってみると原因が分かるかもしれません。

0Like

Your answer might help someone💌