1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter FutureBuilderでcurrentUserが使えなくなってた(Firebase Authentication)

Posted at

環境

pubspec.yaml
environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3
  firebase_core: ^0.7.0
  cloud_firestore: ^0.16.0+1
  firebase_auth: ^0.20.1

The argument type 'Stream' can't be assigned to the parameter type 'Future'.

このエラーは恐らくバージョンの問題かと思われる。。。
非同期の処理を指定するfutureの部分に、currentUserを入れたいのだが、どうもエラーになる。。。:expressionless:

 Widget build(BuildContext context) {
    return FutureBuilder(
        future: FirebaseAuth.instance.currentUser, // ここでエラー
        builder: (ctx, futureSnapshot) {
          if (futureSnapshot.hasData) {
            User user = futureSnapshot.data;
            return Dashboard();
          }
          return LoginScreen();
        });
  }

理由は正直よくわかりませんが、StreamBuilderを使うと同じようなことができるようになりました。

StreamBuilderとauthStateChangesで代用?

 Widget build(BuildContext context) {
    return StreamBuilder(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (ctx, futureSnapshot) {
          if (futureSnapshot.hasData) {
            User user = futureSnapshot.data;
            return Dashboard();
          }
          return LoginScreen();
        });
  }

一応これで同じような?処理にはできたっぽいです。。
なんでFutureBuilderではダメなのか、色々と調べましたが結局よくわからずです。誰かわかりやすく解説してください:sweat_smile:

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?