bino arin
bino arin

Reputation: 183

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),
      ),
    ),

Upvotes: 0

Views: 85

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63864

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

Upvotes: 1

Related Questions