Search code examples
flutterdartdart-null-safety

Null check operator used on a null value in flutter project


In the drawer, I'm showing the user's email address. but null error is showing ! i am also adding ? this sign same error but null

final email = FirebaseAuth.instance.currentUser!.email;


   Center(
      child: Text(
        '$email',
        style: const TextStyle(fontWeight:   FontWeight.bold, fontSize: 18.0, color: Colors.indigo),
      ),
    ),

Solution

  • It is possible your currentUser is null. Instead of forcing with bang!, you can do a null check or accept null value like

    final email = FirebaseAuth.instance.currentUser?.email;
    

    Also you can provide default value like

    final email = FirebaseAuth.instance.currentUser?.email ?? "Got null user";
    

    Find more about null-safety