Rahul Somasundaram
Rahul Somasundaram

Reputation: 596

Stream Initial state is ConnectionState.waiting instead of null

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

I am newbee to Dart, this is my 1st app. I need to display the Username after the signin is succesful.

Whenever I start the application it goes to ConnectionState.waiting instead of ConnectionState.none.

Also it does not render the textbox with displayname on succesful signin. reloading it does render the display name.

Upvotes: 3

Views: 3388

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657416

ConnectionState.none means there is no stream assigned, but you have

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

this means the StreamBuilder subscribed to this stream and waits for events.

Upvotes: 2

Related Questions