Advertisements
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:
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