Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter Null Check Operator used on a null value Firebase Auth

I am getting null check operator used on a null value error when using signout function. Basically if i comment this line out // final user = FirebaseAuth.instance.currentUser!; it works fine but the signout doesn’t work. In short, i want my ui to show with signout function.

Error Image
Error Screenshot
Expected Output
Expected output

Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import 'package:blood/constants/constants.dart';
import 'package:blood/widgets/badge_list.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:blood/widgets/horizontal_list.dart';

class DonorHome extends StatefulWidget {
  const DonorHome({super.key});

  @override
  State<DonorHome> createState() => _DonorHomeState();
}

class _DonorHomeState extends State<DonorHome> {
  final user = FirebaseAuth.instance.currentUser!;

**  // sign user out**
  void signUserOut() {
    FirebaseAuth.instance.signOut();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: bgPink,
      body: SafeArea(
        child: Padding(
          padding: EdgeInsets.all(25.0),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  // Hi, User
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'Hi, User!',
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 24,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(
                        height: 8,
                      ),
                      Text(
                        '11 March 2023',
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 12,
                        ),
                      ),
                    ],
                  ),

                  // Settings Icon
                  Container(
                    decoration: BoxDecoration(
                      color: darkPink,
                      borderRadius: BorderRadius.circular(12),
                    ),
                    padding: EdgeInsets.all(12),
// **this is the signout button**
                    child: Container(
                      child: IconButton(
                        icon: Icon(
                          Icons.logout,
                          color: Colors.white,
                        ),
                        onPressed: signUserOut,
                      ),
                    ),
                  ),
                ],
              ),

              SizedBox(
                height: 50,
              ),
              // User just donated
              // SingleChildScrollView(
              //     scrollDirection: Axis.horizontal,
              //     child: Row(
              //       children: [
              //         UserDonatedCard(),
              //         UserDonatedCard(),
              //         UserDonatedCard(),
              //         UserDonatedCard(),
              //         UserDonatedCard(),
              //       ],
              //     )),
              SizedBox(
                height: 349,
                child: ListView.builder(
                  scrollDirection: Axis.horizontal,
                  physics: const BouncingScrollPhysics(),
                  itemBuilder: (context, index) {
                    return MyHorizontalList(
                      startColor: 0xfff59da4,
                      endColor: 0xffee6b73,
                      courseHeadline: 'Donated 0 mins ago!',
                      courseTitle: 'Username',
                      courseImage: 'lib/images/man.png',
                    );
                  },
                ),
              ),
              SizedBox(
                height: 10,
              ),
              Divider(
                thickness: 2,
                color: darkPink,
              ),
              SizedBox(
                height: 25,
              ),
              Expanded(
                child: ListView(
                  children: [
                    BadgesTile(
                        imageUrl: 'lib/images/badge1.png',
                        title: 'Rescue Hero',
                        subtitle: 'subtitle'),
                    BadgesTile(
                        imageUrl: 'lib/images/badge1.png',
                        title: 'Rescue Hero',
                        subtitle: 'subtitle'),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Auth Service.dart file:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

class AuthService {
  signInWithGoogle() async {
    // begin interactive sign in process
    final GoogleSignInAccount? gUser = await GoogleSignIn().signIn();
    // obtain auth details from request
    final GoogleSignInAuthentication gAuth = await gUser!.authentication;
    // create a new credential for user
    final credential = GoogleAuthProvider.credential(
      accessToken: gAuth.accessToken,
      idToken: gAuth.idToken,
    );
    // sign in
    return await FirebaseAuth.instance.signInWithCredential(credential);
  }
}

Please someone help me

I tried hot restart, tried closing emulator and launching again.

>Solution :

this is becuase there is no user signed in.
FirebaseAuth.instance.currentUser!; is returning null.
first sigin the user and the error will be gone

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading