Home > database >  The body might complete normally, causing 'null' to be returned, but the return type is a
The body might complete normally, causing 'null' to be returned, but the return type is a

Time:10-29

I am currently making a mobile app in flutter, which has a boomMenu, I would like to add a 'FutureBuilder' within this menu, this is possible, when I try to do it I get the following error:

The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type

**This is my code: **

BoomMenu buildBoomMenu() {
  Expanded(child:
  FutureBuilder(
      future: stationSvc.getStations4(http.Client()),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        try {
          if (!snapshot.hasData) {
            return SizedBox(
              height: MediaQuery
                  .of(context)
                  .size
                  .height / 1.3,
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      "   Cargando....",
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 25,
                          fontWeight: FontWeight.normal),
                    ),
                    SizedBox(
                      height: 25,
                    ),
                    SpinKitCubeGrid(
                        color: Color.fromRGBO(16, 71, 115, 1)
                    ),
                  ],
                ),
              ),
            );
          }
          return snapshot.data.length > 0
              ? ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (BuildContext context, int index) {
                return BoomMenu(
                    animatedIcon: AnimatedIcons.menu_close,
                    animatedIconTheme: IconThemeData(size: 152.0),
                    //child: Icon(Icons.add),
                    onOpen: () => print('OPENING DIAL'),
                    onClose: () => print('DIAL CLOSED'),
                    scrollVisible: scrollVisible,
                    overlayColor: Colors.black,
                    elevation: 10,
                    overlayOpacity: 0.7,
                    children: [
                      MenuItemModel(
                        title: snapshot.data[index].devicename!,
                        titleColor: Colors.grey[850]!,
                        subtitle: snapshot.data[index].devicename!,
                        subTitleColor: Colors.grey[850]!,
                        backgroundColor: Colors.grey[50]!,
                        onTap: () => print('THIRD CHILD'),
                        elevation: 10,
                      ),
                      MenuItemModel(
                        title: "List",
                        titleColor: Colors.white,
                        subtitle: "Lorem ipsum dolor sit amet, consectetur",
                        subTitleColor: Colors.white,
                        backgroundColor: Colors.pinkAccent,
                        onTap: () => print('FOURTH CHILD'),
                        elevation: 10,
                      ),
                      MenuItemModel(
                        title: "Team",
                        titleColor: Colors.grey[850]!,
                        subtitle: "Lorem ipsum dolor sit amet, consectetur",
                        subTitleColor: Colors.grey[850]!,
                        backgroundColor: Colors.grey[50]!,
                        onTap: () => print('THIRD CHILD'),
                        elevation: 10,
                      ),
                    ]);
              })

              : Center(
              child: Text('No hay datos, registra un grupo primero'));
        } catch (Exc) {
          print(Exc);
          rethrow;
        }
      }),
  );
}

This is my service 'getStations4()':

Future<List<Stations>> getStations4(http.Client client) async {
final response = await client
    .get(Uri.parse('URL'));

// Use the compute function to run parsePhotos in a separate isolate.
print(compute(parseStations, response.body));
return compute(parseStations, response.body);
}

List<Stations> parseStations(String responseBody) {
final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>> 
();

return parsed.map<Stations>((json) => 
Stations.fromJson(json)).toList();
 }

Images of the boomMenu:

enter image description here

enter image description here

CodePudding user response:

Your function doesn't return anything, it doesn't know where to start. you need to return the expanded widget at the top level, and make the return type Widget. Because if your future fails, its not returning a BoomMenu, its returning a Text widget.

Widget buildBoomMenu() {
  return Expanded(child:
  FutureBuilder(
      future: stationSvc.getStations4(http.Client()),
      builder: (BuildContext context, AsyncSnapshot snapshot) {

CodePudding user response:

Change your buildBoomMenu to this:

Widget buildBoomMenu() { //<--- change this
  return Expanded(child://<--- add this
     FutureBuilder(
      ...
     )
   );
}

your first issue is that you forgot the return keyword, the second one is that your buildBoomMenu return type is BoomMenu but you are returning other widget so I recommended to change it to Widget, like above.

  • Related