gpt4 book ai didi

firebase - Stream 初始状态是 ConnectionState.waiting 而不是 null

转载 作者:IT王子 更新时间:2023-10-29 06:44:14 25 4
gpt4 key购买 nike

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);
}
}

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

每当我启动应用程序时,它都会转到 ConnectionState.waiting 而不是 ConnectionState.none。

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

最佳答案

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

setState(() => _currentUser = signInWithGoogle()?.asStream());

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

关于firebase - Stream 初始状态是 ConnectionState.waiting 而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50385776/

25 4 0