Home > Software design >  The body might complete normally, causing 'null' to be returned
The body might complete normally, causing 'null' to be returned

Time:12-30

  body: FutureBuilder(
  future: determinePosition(),
  builder: (context, snapshot) //error here{
  if (snapshot.connectionState == ConnectionState.waiting) {
  return CircularProgressIndicator();
  } else if (snapshot.hasError) {
  return Text("Error ");
  }else if(snapshot.hasData){
return Column(
children: [
Text(currentAddress),

The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type. why it is wrong ? help me

CodePudding user response:

The potential solution is remove last else if (which is snapshot.hasData) and directly return the the column same as below

  body: FutureBuilder(
  future: determinePosition(),
  builder: (context, snapshot) //error here{
  if (snapshot.connectionState == ConnectionState.waiting) {
  return CircularProgressIndicator();
  } else if (snapshot.hasError) {
  return Text("Error ");
  }
return Column(
children: [
Text(currentAddress),

CodePudding user response:

body: FutureBuilder(
future: determinePosition(),
  builder: (context, snapshot) {
  if (snapshot.connectionState == ConnectionState.waiting) {
  return CircularProgressIndicator();
  } else if (snapshot.hasError) {
  return Text("Error ");
  }
  return Column(
  children: [
Text(currentAddress),
currentposition == true
? Text('Latitude = '   currentposition!.latitude.toString()) //here null (!)
    : Container(),
currentposition == false
? Text('Longitude = '   currentposition!.longitude.toString()) //here null (!)
    : Container(),
TextButton(
onPressed: () {
determinePosition();
},
child: Text('Locate me'))
],
);}),
);
}
}

i want to replace the null (!) because it can't display the longitude and latitude on my page

  • Related