Android Studio 主体可能会正常完成,导致传回'null',但传回型别可能是不可为null的型别(BoomMenu Flutter)

bjp0bcyl  于 2022-11-16  发布在  Android
关注(0)|答案(2)|浏览(155)

我目前正在flutter中制作一个移动的应用程序,它有一个boomMenu,我想在此菜单中添加一个“FutureBuilder”,这是可能的,当我尝试这样做时,我得到以下错误:
主体可能会正常完成,导致传回'null',但传回型别可能是不可为null的型别

这是我的代码:

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

这是我的服务'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();
 }

动臂菜单图片:

wnrlj8wa

wnrlj8wa1#

你的函数没有返回任何东西,它不知道从哪里开始。你需要返回顶层展开的小部件,并使返回类型为Widget。因为如果你的future失败,它不会返回BoomMenu,而是返回一个Text小部件。

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

hts6caw32#

将您的buildBoomMenu更改为:

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

你的第一个问题是你忘记了return关键字,第二个问题是你的buildBoomMenu返回类型是BoomMenu,但你返回的是其他小部件,所以我建议把它改为Widget,就像上面一样。

相关问题