Home > Net >  'Widget', is a potentially non-nullable type. Try adding either a return or a throw statem
'Widget', is a potentially non-nullable type. Try adding either a return or a throw statem

Time:05-28

enter image description here enter image description here

The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

Undefined name 'snapshot'. Try correcting the name to one that is defined, or defining the name.

CodePudding user response:

Update your code like this

 builder: (context, snapshot) {
          return snapshot.hasError ? Container() //your widget : Sizedbox();
        }

CodePudding user response:

Undefined name 'snapshot'. Try correcting the name to one that is defined, or defining the name.

You have used an incorrect spelling of the defined variable, It is defined on line 23 as "Snapshot" and you are using it on line 29 as "snapshot".

You need to use "Snapshot" on line 29 as well.

The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

You need to first understand that the builder argument return type is (context,snapshot) => Widget which means that the builder requires a widget to return out of it.

Currently your code only checks of condition if(snapshot.hasError), in case if there is no error, the builder is receiving null or no widget.

To fix this using your code, here is an example

builder: (context, snapshot) {
      if(snapshot.hasError){
        // Your previous code  
      }

      // In case there is no error, do some work that you want, I used the progress indicator.
      return CircularProgressIndicator();
}

CodePudding user response:

Widget build(BuildContext context) {
return FutureBuilder(
  future: firebase,
  builder: (context, snapshot){
    
    if(snapshot.hasError){
        
        return Scaffold(
          appBar: AppBar(title: Text("Error"),
          ),
          body: Center(
            child: Text("${Snapshot.error}"),),
          );
    }
   })};

sorry bro i'm beginner. can you fix for me pls.

  • Related