Search code examples
flutterdartgeocoding

The body might complete normally, causing 'null' to be returned


  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


Solution

  • The problem is that you're using else if instead of else. By using else if, you're telling Dart to follow this set of conditions, but what if there's another condition? that won't be handled, and will return null.

    To fix the issue, use else instead of else if in the last clause:

    FutureBuilder(
                future: determinePosition(),
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return CircularProgressIndicator();
                  } else if (snapshot.hasError) {
                    return Text("Error ");
                  } else {
                    return Column(
                      children: [
                        Text('test'),
                      ],
                    );
                  }
                })