I am getting data from firebase and making cards using that data. to display certain data of user from database, the data loads fine but for 1 second or more there is a red error screen. I want this to go away I'll show you my code. can somebody please help show me what's wrong. I have no clue what I am doing wrong that this happens and I get the error
The getter 'uid' was called on null. Receiver: null Tried calling: uid
The method '[]' was called on null. Receiver: null Tried calling:
here is my code, any help will be appreciated thankyou
class CardBuilder extends StatefulWidget {
final String title;
final String text;
final IconData icon;
CardBuilder({Key key, this.title,this.text,this.icon}): super(key: key);
@override
_CardBuilderState createState() => _CardBuilderState();
}
class _CardBuilderState extends State<CardBuilder> {
FirebaseUser user;
String error;
void setUser(FirebaseUser user) {
setState(() {
this.user = user;
this.error = null;
});
}
void setError(e) {
setState(() {
this.user = null;
this.error = e.toString();
});
}
@override
void initState() {
super.initState();
FirebaseAuth.instance.currentUser().then(setUser).catchError(setError);
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection(UserInformation).document(user.uid).snapshots(),
builder: (context, snapshot) {
var userDocument = snapshot.data;
// User userDocument here ..........
return Card(
margin: EdgeInsets.only(left: 20,right: 20,top: 20),
child: ListTile(
leading:
Icon(widget.icon, color: QamaiGreen, size: 20.0),
title: AutoSizeText(
'${widget.title} : ${userDocument[widget.text]}',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.w600,
color: QamaiThemeColor,
fontSize: 15
),
maxLines: 1,
maxFontSize: 15,
minFontSize: 12,
),
));
});
}
}
So after trying out some stuff I finally fixed this error. decided to post an answer since this question doesn't have any solution online. at-least I couldn't find one. hope this helps people having the same issue
what I first did was declare a String which contained the value of user.uid
String userid;
void _getUser() async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
userid=user.uid;
}
now in the card builder widget I got rid of the extra stuff and simply called _getUser() method in the init state
class _CardBuilderState extends State<CardBuilder> {
@override
void initState() {
super.initState();
_getUser();
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection(UserInformation).document(userid).snapshots(),
builder: (context, snapshot) {
if(snapshot.hasData)
{
final userDocument = snapshot.data;
final title=userDocument[widget.text];
return Card(
margin: EdgeInsets.only(left: 20,right: 20,top: 20),
child: ListTile(
leading:
Icon(widget.icon, color: QamaiGreen, size: 20.0),
title: AutoSizeText(
'${widget.title} : $title',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.w600,
color: QamaiThemeColor,
fontSize: 15
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
maxFontSize: 15,
minFontSize: 9,
),
));
}
else{
return Card(
margin: EdgeInsets.only(left: 20,right: 20,top: 20),
child: ListTile(
leading:
Icon(widget.icon, color: QamaiGreen, size: 20.0),
title: AutoSizeText(
'LOADING...',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.w600,
color: QamaiThemeColor,
fontSize: 15
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
maxFontSize: 15,
minFontSize: 9,
),
));
}
});
}
}
Last changes were to add a condition checking if the snapshot has data or not. also saving the specific document field in a final variable helped a lot in my case that is
final title=userDocument[widget.text];
and finally an else condition was added to mimic the same card interface that shows a text which says loading. you can use CirculorIndicator if you want that will also work for me this was looking more natural for the interface.
you can show a progress indicator as your load your data as shown below
return StreamBuilder(
stream: Firestore.instance.collection(UserInformation).document(user.uid).snapshots(),
builder: (context, snapshot) {
//==========show progress============
if (!snapshot.hasData) {
return CircularProgressIndicator()
}
var userDocument = snapshot.data;
// User userDocument here ..........
try this in main.dart:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
When the device doesn't have proper internet connection or when the device is offline, flutter red error screen is displayed.
You can try using a CircularProgressIndicator as data loads from the API.
String userid;
void _getUser() async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
userid=user.uid;
}
Ensure you initialize the _getUser method.
@override
void initState() {
getUser();
super.initState();
}
You can now build your widget.
userid == null ? CircularProgressIndicator() : yourWidget(),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.