return FutureBuilder(
future: FirebaseAuto.instance.currentUser(), The function can't be unconditionally invoked because it can be 'null. Try adding a null check ('!').
...
This is the code where I got the error, I tried to add null check operator but could not work
CodePudding user response:
You are getting this error because FirebaseAuth.instance.currentUser()
is not a future function. It returns User?
. Hence, it can't be used as a future
inside FutureBuilder
.
Here's a snippet from its source code:
In your code snippet, I can't figure out why you need this future builder. If you can, remove it completely since you are not using any of its properties. You can return the ListView.builder
that you are using at line number 32 from line number 23.
I hope that helps!
CodePudding user response:
First make variable like this
late<User?> Future user;
Then in side initState function do
@override
initState(){
super.initState();
user = FirebaseAuth.instance.currentUser();
}
then passed inside futureBuilder
like this
FutureBuilder<User?>(
future: user,
builder: (context,child , snapShot){
if(snapShot.data != null)
{
return YOUR_WIDGET ();
}
else
{
return ANY LOADING WIDGET():
}
});
note don't forget to but Data Type for futureBuilder like
futureBuilder<YOUR DATA TYPE>(...)
this for Future in general but you did'nt have to use future builder instate use normal Widget but first get user inside initState
late user;
@override
initState(){
super.initState();
user = FirebaseAuth.instance.currentUser();
}
@override
Widget build(BuildContext context) {
if(user != null){
return YOUR_WIDGET
} else {
return ANY LOADING WIDGET():
}
}
CodePudding user response:
FirebaseAuth.instance.currentUser()
is a getter not a future.
How to go about it ?
Use variable of Type User?
and then once it is not null
show the ListView
User? user;
@override
void initState() {
super.initState();
setState((){
user = FirebaseAuth.instance.currentUser();
}
}
@override
Widget build(BuildContext context) {
return Container(
child: user ? <ListView here> : <CircularProgressIndicator here>
);
}