pika3113
pika3113

Reputation: 65

Null check operator used on a null value (error)

This is the code:

 ​// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables 
  
 ​import​ ​'package:cloud_firestore/cloud_firestore.dart'​; 
 ​import​ ​'package:firebase_auth/firebase_auth.dart'​; 
 ​import​ ​'package:flutter/material.dart'​; 
 ​import​ ​'package:google_nav_bar/google_nav_bar.dart'​; 
 ​import​ ​'package:projectciplified/screens/read%20data/get_user_name.dart'​; 
  
 ​class​ ​HomePage​ ​extends​ ​StatefulWidget​ { 
 ​  ​const​ ​HomePage​({​Key​?​ key}) ​:​ ​super​(key​:​ key); 
  
 ​  ​@override 
 ​  ​State<​HomePage​>​ ​createState​() ​=>​ ​_HomePageState​(); 
 ​} 
  
 ​class​ ​_HomePageState​ ​extends​ ​State<​HomePage​>​ { 
 ​  ​final​ user ​=​ ​FirebaseAuth​.instance.currentUser​!​; 
  
 ​  ​//document ids 
 ​  ​List<​String​>​ docIDs ​=​ []; 
  
 ​  ​// method to get document ids 
 ​  ​Future​ ​getDocID​() ​async​ { 
 ​    ​await​ ​FirebaseFirestore​.instance.​collection​(​'users'​).​get​().​then​( 
 ​          (snapshot) ​=>​ snapshot.docs.​forEach​( 
 ​            (document) { 
 ​              ​print​(document.reference); 
 ​              docIDs.​add​(document.reference.id); 
 ​            }, 
 ​          ), 
 ​        ); 
 ​  } 
  
 ​  ​@override 
 ​  ​Widget​ ​build​(​BuildContext​ context) { 
 ​    ​return​ ​Scaffold​( 
 ​      appBar​:​ ​AppBar​( 
 ​        title​:​ ​Text​( 
 ​          user.email​!​, 
 ​          style​:​ ​TextStyle​(fontSize​:​ ​16​), 
 ​        ), 
 ​        actions​:​ [ 
 ​          ​GestureDetector​( 
 ​              onTap​:​ () { 
 ​                ​FirebaseAuth​.instance.​signOut​(); 
 ​              }, 
 ​              child​:​ ​Icon​(​Icons​.logout)), 
 ​        ], 
 ​      ), 
 ​      bottomNavigationBar​:​ ​Container​( 
 ​        color​:​ ​Colors​.black, 
 ​        child​:​ ​Padding​( 
 ​          padding​:​ ​const​ ​EdgeInsets​.​symmetric​( 
 ​            horizontal​:​ ​15​, 
 ​            vertical​:​ ​20.0​, 
 ​          ), 
 ​          child​:​ ​GNav​( 
 ​            backgroundColor​:​ ​Colors​.black, 
 ​            color​:​ ​Colors​.white, 
 ​            activeColor​:​ ​Colors​.white, 
 ​            tabBackgroundColor​:​ ​Colors​.blue, 
 ​            padding​:​ ​EdgeInsets​.​all​(​10​), 
 ​            gap​:​ ​2​, 
 ​            onTabChange​:​ (index) {}, 
 ​            tabs​:​ [ 
 ​              ​GButton​( 
 ​                icon​:​ ​Icons​.home, 
 ​                text​:​ ​'Home'​, 
 ​              ), 
 ​              ​GButton​( 
 ​                icon​:​ ​Icons​.favorite_border, 
 ​                text​:​ ​'likes'​, 
 ​              ), 
 ​              ​GButton​( 
 ​                icon​:​ ​Icons​.search, 
 ​                text​:​ ​'search'​, 
 ​              ), 
 ​              ​GButton​( 
 ​                icon​:​ ​Icons​.settings, 
 ​                text​:​ ​'settings'​, 
 ​              ), 
 ​            ], 
 ​          ), 
 ​        ), 
 ​      ), 
 ​      body​:​ ​Center​( 
 ​        child​:​ ​Column​( 
 ​          mainAxisAlignment​:​ ​MainAxisAlignment​.center, 
 ​          children​:​ [ 
 ​            ​Expanded​( 
 ​                child​:​ ​FutureBuilder​( 
 ​              future​:​ ​getDocID​(), 
 ​              builder​:​ (context, snapshot) { 
 ​                ​return​ ​ListView​.​builder​( 
 ​                  itemCount​:​ docIDs.length, 
 ​                  itemBuilder​:​ (context, index) { 
 ​                    ​return​ ​Padding​( 
 ​                      padding​:​ ​const​ ​EdgeInsets​.​all​(​8.0​), 
 ​                      child​:​ ​ListTile​( 
 ​                        tileColor​:​ ​Colors​.grey[​200​], 
 ​                        title​:​ ​GetUserName​(documentID​:​ docIDs[index]), 
 ​                      ), 
 ​                    ); 
 ​                  }, 
 ​                ); 
 ​              }, 
 ​            )), 
 ​          ], 
 ​        ), 
 ​      ), 
 ​    ); 
 ​  } 
 ​}

I'm trying to get the user info with ​FirebaseAuth​.instance.currentUser! but it's giving said error. With ​FirebaseAuth​.instance.currentUser (without the !) The code works but why?

Can someone explain the null error? I'm not sure why this code doesn't work as it worked around 5 hours ago. How do I fix it? Thanks for your help.

Upvotes: 0

Views: 193

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63594

There are few states FutureBuilder provides, like connection state, error state and data or empty state. While you calling future it needs some time to fetch data, means first state is loading state and data is not ready yet. That's why it says null error because we don't have any data.

Do like

FutureBuilder(
  future: ​getDocID​(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return Center(child: Text('Please wait its loading...'));
    } else if (snapshot.hasError) {
      return Center(child: Text('Error: ${snapshot.error}'));
    } else if (snapshot.hasData) {
      return ListView(...);
    }
    else return Text("dont have any data")
  },

Find more about FutureBuilder on flutter.dev.

Upvotes: 1

Related Questions