简体   繁体   中英

The argument type 'User?' can't be assigned to the parameter type 'Future<Object?>?'., when i can the code got this error

代码在这里

return FutureBuilder(
    future: FirebaseAuth.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

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!

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():

  }
}

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>
    );
  }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM