flutter 正文可能正常完成,导致返回“null”,但返回类型、错误、如何在此处解决它

xuo3flqw  于 2023-01-14  发布在  Flutter
关注(0)|答案(1)|浏览(297)

知道这个问题的函数需要返回一个小部件,但现在确定我如何才能解决这个问题在我的代码,感谢帮助

class PhotoGrid extends StatefulWidget {
  const PhotoGrid({super.key});

  @override
  State<PhotoGrid> createState() => _PhotoGridState();
}

class _PhotoGridState extends State<PhotoGrid> {
  final Stream<QuerySnapshot> _photoStream =
      FirebaseFirestore.instance.collection('photos').snapshots();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StreamBuilder<QuerySnapshot>(
          stream: _photoStream,
          builder:
              (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasError) {
              return const Text('Something went wrong');
            }

            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Text("Loading");
            }

            return GridView.builder(
              gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                maxCrossAxisExtent: 120,
                crossAxisSpacing: 20,
                childAspectRatio: 3 / 2,
                mainAxisSpacing: 20,
              ),
              itemCount: snapshot.data!.docs.length,
              itemBuilder: (context, index) {
                var data = snapshot.data!.docs
                    .map((DocumentSnapshot documents) {
                      var data = documents.data()! as Map<String, dynamic>;
                      return Container(
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: NetworkImage('${data['ImgUrl']}'),
                          ),
                        ),
                      );
                    })
                    .toList()
                    .cast();
              },
            );
          },
        ),
      ),
    );
  }
}
enter code here

知道函数需要返回一个小部件的问题,但现在确定如何在我的代码中解决这个问题,感谢帮助知道函数需要返回一个小部件的问题,但现在确定如何在我的代码中解决这个问题,感谢帮助

ubof19bj

ubof19bj1#

return GridView.builder(
  gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
    maxCrossAxisExtent: 120,
    crossAxisSpacing: 20,
    childAspectRatio: 3 / 2,
    mainAxisSpacing: 20,
  ),
  itemCount: snapshot.data!.docs.length,
  itemBuilder: (context, index) {
    var data = snapshot.data!.docs[index].data()! as Map<String, dynamic>;
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: NetworkImage('${data['ImgUrl']}'),
        ),
      ),
    );
  },
);

该错误是由GridView.builder小部件中的项目构建器函数未返回任何内容而应返回一个小部件这一事实引起的。为了修复该问题,修改了该函数以返回一个容器小部件,其中包含一个DecorationImage,该DecorationImage显示来自Firebase查询的数据Map的ImgUrl键的图像。

相关问题