0
  FirebaseUser loggedinuser;
  FirebaseAuth _auth = FirebaseAuth.instance;

  Future<void> currentUser()async
  {
    try
    {
      FirebaseUser user = await _auth.currentUser();
      if(user != null)
      {
        setState(() {
          loggedinuser  = user;
        });
      }
    }
    catch(e){
      print(e);
    }
  }
  @override
  void initState() {
    super.initState();
    currentUser();
  }



@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Logged in"),
        centerTitle: true,
        backgroundColor: Colors.green,
      ),
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 20,vertical: 20),
        child: Text(loggedinuser.providerId)

Hi im new in FLutter and was working on a little project to practice but whenever i try to show name of user i get an error saying that Text widget cant be null but im initializing the loggedinuser by using setState i can see email but not the name on screen Would appreciate the help

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

2 Answers2

0

I think you should use a FutureBuilder like the @nvoigt said. But you can also fix that changing this line Text(loggedinuser.providerId) to this:

Text(loggedinuser != null loggedinuser.providerId : "Loading / Not Logged in")

Using a FutureBuilder you would get something like this:

class LoadUser extends StatefulWidget {
  @override
  _LoadUserState createState() => _LoadUserState();
}

class _LoadUserState extends State<LoadUser> {
  FirebaseUser loggedinuser;
  FirebaseAuth _auth = FirebaseAuth.instance;

  Future<FirebaseUser> currentUser() async {
    try {
      FirebaseUser user = await _auth.currentUser();
      if (user != null) {
        return user;
      }
      return null;
    } catch (e) {
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("FirebaseUser"),
        centerTitle: true,
        backgroundColor: Colors.green,
      ),
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
        child: FutureBuilder<FirebaseUser>(
          future: currentUser(),
          builder: (context, snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.waiting:
              case ConnectionState.active:
                return Center(child: CircularProgressIndicator(),);
                break;
              case ConnectionState.done:
                loggedinuser = snapshot.data;
                return Text(loggedinuser.providerId);
                break;
              default:
                return Text('Not logged in :(');
                break;
            }
          },
        )
      ),
    );
  }
}
Vitor
  • 762
  • 6
  • 26
0

Your currentUser() function is asynchronous, it takes time to instantiate loggedinuser. So initially loggedinuser is null, thats why you are getting the error. To come up with the solution, wrap Text widget with FutureBuilder.

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Logged in"),
        centerTitle: true,
        backgroundColor: Colors.green,
      ),
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
        child: FutureBuilder(
          future: currentUser(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.active) {
              return Text(loggedInUser.providerId.toString());
            } else if (snapshot.connectionState == ConnectionState.waiting) {
              return CircularProgressIndicator();
            }
          },
        ),
      ),
    );
  }
Newaj
  • 3,992
  • 4
  • 32
  • 50
  • Thank You just wan to ask can i use Streambuilder instead of Futurebuilder?? – Abdul Munim Aug 08 '20 at 12:41
  • If your `currentUser` function returns `Stream`, then you can use `StreamBuilder`. As it is returning `Future` , you can go with `FutureBuilder`. If my answer solves your problem, then accept the answer. – Newaj Aug 08 '20 at 13:58