I can display profile image but every time home_screen dart reloads i get this error. i tried without null check but then my code doesn't work.
Error
Null check operator used on a null value
My code in home_screen.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import '../models/user_model.dart';
import 'edit_profile.dart';
import 'splash_screen.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
User? user = FirebaseAuth.instance.currentUser;
UserModel? logginUserModel;
getUserData() {
FirebaseFirestore.instance
.collection("users")
.doc(user!.uid)
.get()
.then((value) {
logginUserModel = UserModel.fromMap(value.data());
setState(() {});
});
}
@override
initState() {
super.initState();
getUserData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Home"),
actions: [
IconButton(
onPressed: () async {
await FirebaseAuth.instance.signOut();
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (ctx) => SplashScreen()),
(route) => false);
},
icon: const Icon(Icons.logout)),
IconButton(
onPressed: () async {
Navigator.push(context,
MaterialPageRoute(builder: (ctx) => EditProfile()));
},
icon: const Icon(Icons.edit)),
],
),
body: Center(
child: Container(
height: 125.0,
width: 125.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(62.5),
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
logginUserModel!.profileImage == null
? "https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png"
: logginUserModel!.profileImage!,
),
),
),
),
),
);
}
}
Error trace of the code
════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building HomeScreen(dirty, state: _HomeScreenState#772d2):
Null check operator used on a null value
The relevant error-causing widget was
HomeScreen
When the exception was thrown, this was the stack
#0 _HomeScreenState.build
#1 StatefulElement.build
#2 ComponentElement.performRebuild
#3 StatefulElement.performRebuild
#4 Element.rebuild
#5 ComponentElement._firstBuild
#6 StatefulElement._firstBuild
#7 ComponentElement.mount
... Normal element mounting (24 frames)
#31 Element.inflateWidget
#32 MultiChildRenderObjectElement.inflateWidget
#33 MultiChildRenderObjectElement.mount
... Normal element mounting (175 frames)
#208 Element.inflateWidget
#209 MultiChildRenderObjectElement.inflateWidget
#210 Element.updateChild
#211 RenderObjectElement.updateChildren
#212 MultiChildRenderObjectElement.update
#213 Element.updateChild
#214 ComponentElement.performRebuild
#215 StatefulElement.performRebuild
#216 Element.rebuild
#217 StatefulElement.update
#218 Element.updateChild
#219 ComponentElement.performRebuild
#220 Element.rebuild
#221 ProxyElement.update
#222 Element.updateChild
#223 ComponentElement.performRebuild
#224 Element.rebuild
#225 ProxyElement.update
#226 _InheritedNotifierElement.update
#227 Element.updateChild
#228 SingleChildRenderObjectElement.update
#229 Element.updateChild
#230 ComponentElement.performRebuild
#231 StatefulElement.performRebuild
#232 Element.rebuild
#233 StatefulElement.update
#234 Element.updateChild
#235 SingleChildRenderObjectElement.update
#236 Element.updateChild
#237 SingleChildRenderObjectElement.update
#238 Element.updateChild
#239 ComponentElement.performRebuild
#240 Element.rebuild
#241 ProxyElement.update
#242 Element.updateChild
#243 ComponentElement.performRebuild
#244 StatefulElement.performRebuild
#245 Element.rebuild
#246 BuildOwner.buildScope
#247 WidgetsBinding.drawFrame
#248 RendererBinding._handlePersistentFrameCallback
#249 SchedulerBinding._invokeFrameCallback
#250 SchedulerBinding.handleDrawFrame
#251 SchedulerBinding._handleDrawFrame
#255 _invoke (dart:ui/hooks.dart:151:10)
#256 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#257 _drawFrame (dart:ui/hooks.dart:115:31)
(elided 3 frames from dart:async)
════════════════════════════════════════════════════════════════════════════════
Reloaded 6 of 927 libraries in 352ms.
CodePudding user response:
The issue lies in this line.
logginUserModel!.profileImage == null
Change it to loggingUserModel?.profileImage == null
.
CodePudding user response:
Can you remove setState(() {})
from getUserData()
function and try again?
I think you could also use FutureBuilder
to get your image safely