The getter 'uid' was called on null. Receiver: null Tried calling: uid Flutter Firebase
211
Most likely the problem is here:
ProfilePage(userProfileId: currentUser.uid),
You're trying to pass currentUser.uid
to the ProfilePage
, but initially the currentUser
is going to be null
. So that explains the error.
The solution is to only render the ProfilePage
when there is a current user, for example with:
currentUser != null ? ProfilePage(userProfileId: currentUser.uid) : Text("Loading user data..."),
Comments
-
Soner Karaevli over 1 year
i am sending userID to Profile Page from Selector Page. The error appears once and then disappears. But the app works. Debug console say the error is related with the selectorPage. I think there's a problem in the userInfo function.
here is my selectorPage: I am sending the currentUser to ProfilePage
final FirebaseAuth auth = FirebaseAuth.instance; User1 currentUser; class SelectorPage extends StatefulWidget { SelectorPage({Key key}) : super(key: key); @override _SelectorPageState createState() => _SelectorPageState(); } class _SelectorPageState extends State<SelectorPage> { int _selectedIndex = 0; PageController _pageController; AuthService authService = new AuthService(); Future<void> userInfo() async { print("It's work!"); final User gcurrentUser = auth.currentUser; DocumentSnapshot documentSnapshot = await usersReference.doc(gcurrentUser.uid).get(); if (!documentSnapshot.exists) { print("there's no data"); } setState(() { currentUser = User1.fromDocument(documentSnapshot); }); } @override void initState() { setState(() { userInfo(); }); super.initState(); _pageController = PageController(); } @override void dispose() { _pageController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: SizedBox.expand( child: PageView( controller: _pageController, onPageChanged: (index) { setState(() => _selectedIndex = index); }, children: <Widget>[ HomePage(), ChattingPage(), NotificationPage(), ProfilePage(userProfileId: currentUser.uid), ], ), ), bottomNavigationBar: BottomNavigationBar( currentIndex: _selectedIndex, selectedItemColor: Colors.white, unselectedItemColor: Colors.grey[600], backgroundColor: mainColor2, onTap: _onItemTapped, items: <BottomNavigationBarItem>[ BottomNavigationBarItem( label: '', icon: Icon(Icons.home), backgroundColor: mainColor2), BottomNavigationBarItem( label: '', icon: Icon(Icons.chat), backgroundColor: mainColor2), BottomNavigationBarItem( label: '', icon: Icon(Icons.new_releases), backgroundColor: mainColor2), BottomNavigationBarItem( label: '', icon: Icon(Icons.account_circle_outlined), backgroundColor: mainColor2), ], ), ); } void _onItemTapped(int index) { setState(() { _selectedIndex = index; userInfo(); _pageController.animateToPage(index, duration: Duration(milliseconds: 500), curve: Curves.easeOut); }); } }
and Profile Page:
class ProfilePage extends StatefulWidget { final String userProfileId; ProfilePage({this.userProfileId}); @override _ProfilePageState createState() => _ProfilePageState(); } class _ProfilePageState extends State<ProfilePage> { AuthService authService = new AuthService(); final String currentOnlineUserId = currentUser?.uid; @override void initState() { super.initState(); } createProfileTopView() { return FutureBuilder( future: usersReference.doc(widget.userProfileId).get(), builder: (context, dataSnapshot) { if (!dataSnapshot.hasData) { return Center( child: Lottie.asset('assets/images/1.json'), ); } User1 user = User1.fromDocument(dataSnapshot.data); return Padding( padding: context.paddingLow, child: Column( children: [ Row( children: [ CircleAvatar( radius: 45.0, backgroundColor: Colors.grey, backgroundImage: CachedNetworkImageProvider(user.photoLink), ), Expanded( child: Column( children: [ Row( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ createColumns("Gönderi", 0), createColumns("Takipçi", 0), createColumns("Takip", 0), ], ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [createButton()], ), ], )) ], ), ], ), ); }); } Column createColumns(String title, int count) { return Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ Text( count.toString(), style: TextStyle( fontWeight: FontWeight.w300, color: Colors.white, fontSize: 20.0), ), Container( margin: EdgeInsets.only(top: 5.0), child: Text( title, style: TextStyle( fontWeight: FontWeight.w300, color: Colors.white, fontSize: 16.0), ), ) ], ); } createButton() { bool ownProfile = currentOnlineUserId == widget.userProfileId; if (ownProfile) { return createButtonTitleAndFunction( title: "Profili düzenle", performFunction: editUserProfile); } } createButtonTitleAndFunction({String title, Function performFunction}) { return Container( padding: EdgeInsets.only(top: 3.0), child: FlatButton( onPressed: () {}, child: Container( width: 245.0, height: 26.0, child: Text( title, style: TextStyle( fontWeight: FontWeight.w300, color: Colors.grey, ), ), alignment: Alignment.center, decoration: BoxDecoration( color: Colors.black, border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(6.0), ), ), ), ); } editUserProfile() { Navigator.push( context, MaterialPageRoute( builder: (context) => EditProfilePage(currentOnlineUserId: currentOnlineUserId))); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: mainColor, appBar: AppBar( backgroundColor: mainColor2, title: Text( "Profil", style: TextStyle(fontWeight: FontWeight.w300), ), actions: [ IconButton( icon: Icon(Icons.logout), onPressed: () { authService.signOut(); Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginPage())); }) ], ), body: ListView( children: [ createProfileTopView(), ], )); } }
Also my UserModal:
class User1 { final String bio; final String email; final String linkedinLink; final String name; final String password; final String photoLink; final String uid; final String university; final String username; User1({ this.bio, this.email, this.linkedinLink, this.name, this.password, this.photoLink, this.uid, this.university, this.username, }); factory User1.fromDocument(DocumentSnapshot docx) { return User1( bio: docx['bio'], email: docx['email'], linkedinLink: docx['linkedinLink'], name: docx['name'], password: docx['password'], photoLink: docx['photoLink'], uid: docx.id, university: docx['university'], username: docx['username'], ); } }
-
Soner Karaevli almost 3 yearsappreciate sir it's worked :) we're following on firebase youtube channel and big fan of you :)