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;

      child: Text(
        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

Leave a Reply Cancel reply