Lalit Rawat
Lalit Rawat

Reputation: 1272

firebase user number is showing null

i want to display the user phone number to the screen but it is showing null on the screen

String _userId;

  @override
  Widget build(BuildContext context) {
    FirebaseAuth.instance.currentUser().then((user) {
      _userId = user.phoneNumber;
    });
    print(_userId);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                child:  Text("$_userId")
                    ?? CircularProgressIndicator(),

Upvotes: 0

Views: 53

Answers (3)

Peter Haddad
Peter Haddad

Reputation: 80914

You need to use a FutureBuilder, the FutureBuilder widget comes with Flutter and makes it easy to work with async data sources. Therefore you need to do the following:

 @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                child : FutureBuilder(
                  future: FirebaseAuth.instance.currentUser(),
                  builder: (context, snapshot) {
                          if (snapshot.hasData) {
                               print(snapshot);
                    return Text(snapshot.data.uid);
                    }
                    // By default, show a loading spinner.
                    return CircularProgressIndicator();
                  }
                )

The future property will contain the asynchronous computation to which this builder is currently connected, possibly null. Also you should be using a Stateful Widget.

Upvotes: 2

Jack
Jack

Reputation: 1163

You should show more of your code but it appears you're using a StatelessWidget which means that after that Future returns, the value of _userId in the build method is not going to update by itself. The simplest way would be to use a StatefulWidget and call setState within your Future to update the value.

Also your line Text("$_userId")?? CircularProgressIndicator() won't work correctly because of the same issue, but you need to show one widget or the other depending on the value of _userId.

Upvotes: 1

J. S.
J. S.

Reputation: 9625

You need to move your Firebase query to outside your build method. The build method is constantly being run as your Widget is rebuilt. Ideally you would implement it this way:

// _userId needs to be initiated with a blank value
String _userId = '';

@override
void initState() {
  loadCurrentUser();
  super.initState();
}

void loadCurrentUser(){
  FirebaseAuth.instance.currentUser().then((user) {
    setState(() {
      _userId = user.phoneNumber;
    });
  });
}

This way, as soon as the information is loaded your view will be updated.

Upvotes: 2

Related Questions