Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
流的初始状态为ConnectionState.waiting,而不是null 导入'dart:async'; 进口“包装:颤振/材料.省道”; 导入“包:firebase_auth/firebase_auth.dart”; 导入“包:google_sign_in/google_sign_in.dart”; final FirebaseAuth _auth=FirebaseAuth.instance; 最终GoogleSignIn_GoogleSignIn=新GoogleSignIn(); 类主页扩展了StatefulWidget{ 静态字符串标记='主页'; @凌驾 HomePageState createState(){ 返回新的HomePageState(); } } 类HomePageState扩展了状态{ 流currentUser; 未来登录使用Google()异步{ 打印(‘谷歌登录’); GoogleSignInAccount googleUser=wait_googleSignIn.signIn(); GoogleSignInAuthentication googleAuth=等待googleUser.authentication; FirebaseUser用户=使用Google等待_auth.signin( accessToken:googleAuth.accessToken, idToken:googleAuth.idToken, ); 打印(“登录”+user.displayName); 返回用户; } @凌驾 小部件构建(构建上下文){ 最终lblWelcome=填充( 填充:边缘设置。全部(8.0), 子项:文本(“Frnds付款”, 样式:TextStyle(fontSize:28.0,颜色:Colors.green)); 最终btnSignin=新材料按钮( 子项:const Text('Sign In with Google'), 已按下:(){ 打印(“点击按钮”); _currentUser=signInWithGoogle()?.asStream(); }); 最终txtOutput=新StreamBuilder( 流:_currentUser, 生成器:(BuildContext上下文,异步快照){ 交换机(快照.连接状态){ 案例连接状态。无: 案例连接状态。正在等待: 返回新中心(子项:new CircularProgressIndicator()); 违约: if(snapshot.hasError) 返回新文本('Error:${snapshot.Error}'); 其他的 返回新中心( 子级:新文本('${snapshot.data.displayName}'); } }); 最终主体=中心( 子:列( mainAxisAlignment:mainAxisAlignment.center, 儿童:[lblWelcome,btnSignin,txtOutput], ), ); 返回脚手架(主体:主体); } }_Firebase_Dart_Firebase Authentication_Flutter - Fatal编程技术网

流的初始状态为ConnectionState.waiting,而不是null 导入'dart:async'; 进口“包装:颤振/材料.省道”; 导入“包:firebase_auth/firebase_auth.dart”; 导入“包:google_sign_in/google_sign_in.dart”; final FirebaseAuth _auth=FirebaseAuth.instance; 最终GoogleSignIn_GoogleSignIn=新GoogleSignIn(); 类主页扩展了StatefulWidget{ 静态字符串标记='主页'; @凌驾 HomePageState createState(){ 返回新的HomePageState(); } } 类HomePageState扩展了状态{ 流currentUser; 未来登录使用Google()异步{ 打印(‘谷歌登录’); GoogleSignInAccount googleUser=wait_googleSignIn.signIn(); GoogleSignInAuthentication googleAuth=等待googleUser.authentication; FirebaseUser用户=使用Google等待_auth.signin( accessToken:googleAuth.accessToken, idToken:googleAuth.idToken, ); 打印(“登录”+user.displayName); 返回用户; } @凌驾 小部件构建(构建上下文){ 最终lblWelcome=填充( 填充:边缘设置。全部(8.0), 子项:文本(“Frnds付款”, 样式:TextStyle(fontSize:28.0,颜色:Colors.green)); 最终btnSignin=新材料按钮( 子项:const Text('Sign In with Google'), 已按下:(){ 打印(“点击按钮”); _currentUser=signInWithGoogle()?.asStream(); }); 最终txtOutput=新StreamBuilder( 流:_currentUser, 生成器:(BuildContext上下文,异步快照){ 交换机(快照.连接状态){ 案例连接状态。无: 案例连接状态。正在等待: 返回新中心(子项:new CircularProgressIndicator()); 违约: if(snapshot.hasError) 返回新文本('Error:${snapshot.Error}'); 其他的 返回新中心( 子级:新文本('${snapshot.data.displayName}'); } }); 最终主体=中心( 子:列( mainAxisAlignment:mainAxisAlignment.center, 儿童:[lblWelcome,btnSignin,txtOutput], ), ); 返回脚手架(主体:主体); } }

流的初始状态为ConnectionState.waiting,而不是null 导入'dart:async'; 进口“包装:颤振/材料.省道”; 导入“包:firebase_auth/firebase_auth.dart”; 导入“包:google_sign_in/google_sign_in.dart”; final FirebaseAuth _auth=FirebaseAuth.instance; 最终GoogleSignIn_GoogleSignIn=新GoogleSignIn(); 类主页扩展了StatefulWidget{ 静态字符串标记='主页'; @凌驾 HomePageState createState(){ 返回新的HomePageState(); } } 类HomePageState扩展了状态{ 流currentUser; 未来登录使用Google()异步{ 打印(‘谷歌登录’); GoogleSignInAccount googleUser=wait_googleSignIn.signIn(); GoogleSignInAuthentication googleAuth=等待googleUser.authentication; FirebaseUser用户=使用Google等待_auth.signin( accessToken:googleAuth.accessToken, idToken:googleAuth.idToken, ); 打印(“登录”+user.displayName); 返回用户; } @凌驾 小部件构建(构建上下文){ 最终lblWelcome=填充( 填充:边缘设置。全部(8.0), 子项:文本(“Frnds付款”, 样式:TextStyle(fontSize:28.0,颜色:Colors.green)); 最终btnSignin=新材料按钮( 子项:const Text('Sign In with Google'), 已按下:(){ 打印(“点击按钮”); _currentUser=signInWithGoogle()?.asStream(); }); 最终txtOutput=新StreamBuilder( 流:_currentUser, 生成器:(BuildContext上下文,异步快照){ 交换机(快照.连接状态){ 案例连接状态。无: 案例连接状态。正在等待: 返回新中心(子项:new CircularProgressIndicator()); 违约: if(snapshot.hasError) 返回新文本('Error:${snapshot.Error}'); 其他的 返回新中心( 子级:新文本('${snapshot.data.displayName}'); } }); 最终主体=中心( 子:列( mainAxisAlignment:mainAxisAlignment.center, 儿童:[lblWelcome,btnSignin,txtOutput], ), ); 返回脚手架(主体:主体); } },firebase,dart,firebase-authentication,flutter,Firebase,Dart,Firebase Authentication,Flutter,我是Dart的新手,这是我的第一个应用程序。 我需要在登录成功后显示用户名 无论何时启动应用程序,它都会转到ConnectionState.waiting,而不是ConnectionState.none 此外,它不会在成功登录时呈现带有displayname的文本框。重新加载它会呈现显示名称。ConnectionState.none表示没有分配流, 但是你有 import 'dart:async'; import 'package:flutter/material.dart'; import

我是Dart的新手,这是我的第一个应用程序。 我需要在登录成功后显示用户名

无论何时启动应用程序,它都会转到ConnectionState.waiting,而不是ConnectionState.none


此外,它不会在成功登录时呈现带有displayname的文本框。重新加载它会呈现显示名称。

ConnectionState.none
表示没有分配流, 但是你有

import 'dart:async';

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

final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = new GoogleSignIn();

class HomePage extends StatefulWidget {
  static String tag = 'home-page';

  @override
  HomePageState createState() {
    return new HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  Stream<FirebaseUser> _currentUser;

  Future<FirebaseUser> signInWithGoogle() async {
    print('google signin invoked');

    GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    GoogleSignInAuthentication googleAuth = await googleUser.authentication;
    FirebaseUser user = await _auth.signInWithGoogle(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    print("signed in " + user.displayName);

    return user;
  }

  @override
  Widget build(BuildContext context) {
    final lblWelcome = Padding(
        padding: EdgeInsets.all(8.0),
        child: Text('Frnds Payment',
            style: TextStyle(fontSize: 28.0, color: Colors.green)));

    final btnSignin = new MaterialButton(
        child: const Text('Sign In with Google'),
        onPressed: () {
          print('button clicked');
          _currentUser = signInWithGoogle()?.asStream();
        });
    final txtOutput = new StreamBuilder<FirebaseUser>(
        stream: _currentUser,
        builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
            case ConnectionState.waiting:
              return new Center(child: new CircularProgressIndicator());
            default:
              if (snapshot.hasError)
                return new Text('Error: ${snapshot.error}');
              else
                return new Center(
                    child: new Text('${snapshot.data.displayName}'));
          }
        });

    final body = Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[lblWelcome, btnSignin, txtOutput],
      ),
    );

    return Scaffold(body: body);
  }
}

这意味着
StreamBuilder
订阅此流并等待事件。

为什么不使用
FutureBuilder
而不是
StreamBuilder
,将
signInWithGoogle()
返回的
Future
转换为
stream
?AsyncSnapshot提供什么数据。你试过为每次调用打印它们吗?FutureBuilder在返回对象时不起作用,在返回字符串时效果很好,而且我需要在signin进行时进行操作。signInWithGoogle()函数在按钮单击时执行,但AsyncSnapshot在生命周期中仅调用一次,如果我软重新加载应用程序,然后用户名显示在文本框中,我不认为这有什么区别。所有的
asStream
所做的就是创建一个流,当底层的未来完成,然后完成时,它会发出一个事件。我希望它最初是一个
流。然后,流向订阅服务器发送值。正在使用Google()删除
登录?.asStream()中的
不应该导致错误。耶!移除?很好,但我在onPressed事件中使用StreamBuilder,对吗?它是如何被应用程序Startup所接受的?我现在明白你的意思了。是,
\u currentUser
在最初加载时应为
null
。您可能希望将
\u currentUser=signInWithGoogle()?.asStream()包装起来带有
设置状态(()=>_currentUser=signInWithGoogle()?.asStream())使用setState()包装在登录后更改了状态。。谢谢你的回答
setState(() => _currentUser = signInWithGoogle()?.asStream());