flutter 我的apk在这里看不到任何firebase数据,StateError(Bad state:快照既无数据也无错误)

nzk0hqpo  于 2023-10-22  发布在  Flutter
关注(0)|答案(1)|浏览(106)

所以,我有麻烦,只有一个收集,另一个工程罚款。我真的不明白我能在这里做什么:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

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

  @override
  State<DarhanScreen> createState() => _DarhanScreenState();
}

class _DarhanScreenState extends State<DarhanScreen> {
  int selectedIndex = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: StreamBuilder<QuerySnapshot>(
          
        
            stream: FirebaseFirestore.instance.collection('posts').snapshots(),
            builder: (context, snapshot) {
             
              return Column(
                
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  
                  const Align(
                    alignment: Alignment.centerRight,
                    child: Icon(Icons.settings),
                  ),  
                  
                  Row(
                    children: [
                      const CircleAvatar(
                        radius: 30,
                      ),
                      const SizedBox(
                        width: 10,
                      ),
                      RichText(
                        text: const TextSpan(
                            text: 'Привет, ',
                            style: TextStyle(
                                color: Colors.black,
                                fontSize: 24,
                                fontFamily: 'onest'),
                            children: [
                              TextSpan(
                                  text: 'Дархан',
                                  style:
                                      TextStyle(fontWeight: FontWeight.bold)),
                              TextSpan(
                                  text: '\nУдачной учебы!',
                                  style: TextStyle(
                                      fontSize: 8,
                                      color: Color.fromRGBO(164, 172, 183, 1)))
                            ]),
                      )
                    ],
                  ),
                   const Padding(
                padding: EdgeInsetsDirectional.fromSTEB(20, 0, 0, 15),
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    'Мероприятия',
                    style: TextStyle(
                        fontSize: 24,
                        fontFamily: 'onest',
                        fontWeight: FontWeight.w800),
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
                  Row(
                    
                    children: [
                      GestureDetector(
                          onTap: () {
                            setState(() {
                              selectedIndex = 0;
                            });
                          },
                          child: Text('Сегодня',
                              style: (selectedIndex == 0)
                                  ? const TextStyle(color: Colors.blue)
                                  : const TextStyle(color: Colors.black))),
                      const SizedBox(
                        width: 10,
                      ),
                      GestureDetector(
                        onTap: () {
                          setState(() {
                            selectedIndex = 1;
                          });
                        },
                        child: Text('Планируется',
                            style: (selectedIndex == 1)
                                ? const TextStyle(color: Colors.blue)
                                : const TextStyle(color: Colors.black)),
                      ),
                      const SizedBox(
                        width: 10,
                      ),
                      GestureDetector(
                        onTap: () {
                          setState(() {
                            selectedIndex = 2;
                          });
                        },
                        child: Text('Прошло',
                            style: (selectedIndex == 2)
                                ? const TextStyle(color: Colors.blue)
                                : const TextStyle(color: Colors.black)),
                      )
                    ],
                  ),
                  SizedBox(
                    height: 200,
                    child: ListView.builder(
                      shrinkWrap: true,
                      scrollDirection: Axis.horizontal,
                      itemCount: snapshot.requireData.size,
                      itemBuilder: (context, index) {
                        if (selectedIndex == 0 &&
                            (DateFormat('yyyy-MM-dd')
                                    .format(DateTime.now())
                                    .toString() ==
                                DateFormat('yyyy-MM-dd')
                                    .format(snapshot
                                        .requireData.docs[index]['start_time']
                                        .toDate())
                                    .toString())) {
                          return Row(
                            children: [
                              const SizedBox(width: 10),
                              GestureDetector(
                                onTap: () {
                                  Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) => DescScreen(
                                              description: snapshot.requireData
                                                  .docs[index]['description'],
                                              end_time: snapshot.requireData.docs[index]
                                                  ['end_time'],
                                              name: snapshot.requireData.docs[index]
                                                  ['name'],
                                              org: snapshot.requireData.docs[index]
                                                  ['org'],
                                              photo: snapshot.requireData.docs[index]
                                                  ['photo'],
                                              place: snapshot.requireData.docs[index]
                                                  ['place'],
                                              start_time: snapshot.requireData.docs[index]
                                                  ['start_time'])));
                                },
                                child: Container(
                                  height: 200,
                                  width: 150,
                                  decoration: BoxDecoration(
                                      image: DecorationImage(
                                          image: NetworkImage(snapshot
                                              .requireData
                                              .docs[index]['photo']),
                                          fit: BoxFit.cover),
                                      borderRadius: BorderRadius.circular(20)),
                                  child: Align(
                                    alignment: Alignment.bottomLeft,
                                    child: Padding(
                                      padding: const EdgeInsets.only(
                                          bottom: 10, left: 10, right: 10),
                                      child: Text(
                                          snapshot.requireData.docs[index]
                                              ['name'],
                                          style: const TextStyle(
                                              color: Colors.white,
                                              fontSize: 15)),
                                    ),
                                  ),
                                ),
                              ),
                              const SizedBox(width: 10)
                            ],
                          );
                        }
                        if (selectedIndex == 2 &&
                            (DateTime.now().day >
                                snapshot.requireData.docs[index]['start_time']
                                    .toDate()
                                    .day)) {
                          return Row(
                            children: [
                              const SizedBox(width: 10),
                              GestureDetector(
                                onTap: () {
                                  Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) => DescScreen(
                                              description: snapshot.requireData
                                                  .docs[index]['description'],
                                              end_time: snapshot.requireData.docs[index]
                                                  ['end_time'],
                                              name: snapshot.requireData.docs[index]
                                                  ['name'],
                                              org: snapshot.requireData.docs[index]
                                                  ['org'],
                                              photo: snapshot.requireData.docs[index]
                                                  ['photo'],
                                              place: snapshot.requireData.docs[index]
                                                  ['place'],
                                              start_time: snapshot.requireData.docs[index]
                                                  ['start_time'])));
                                },
                                child: Container(
                                  height: 200,
                                  width: 150,
                                  decoration: BoxDecoration(
                                      image: DecorationImage(
                                          image: NetworkImage(snapshot
                                              .requireData
                                              .docs[index]['photo']),
                                          fit: BoxFit.cover),
                                      borderRadius: BorderRadius.circular(20)),
                                  child: Align(
                                    alignment: Alignment.bottomLeft,
                                    child: Padding(
                                      padding: const EdgeInsets.only(
                                          bottom: 10, left: 10, right: 10),
                                      child: Text(
                                          snapshot.requireData.docs[index]
                                              ['name'],
                                          style: const TextStyle(
                                              color: Colors.white,
                                              fontSize: 15)),
                                    ),
                                  ),
                                ),
                              ),
                              const SizedBox(width: 10)
                            ],
                          );
                        }
                        if (selectedIndex == 1 &&
                            (DateTime.now().day <
                                snapshot.requireData.docs[index]['start_time']
                                    .toDate()
                                    .day)) {
                          return Row(
                            children: [
                              const SizedBox(width: 10),
                              GestureDetector(
                                onTap: () {
                                  Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) => DescScreen(
                                              description: snapshot.requireData
                                                  .docs[index]['description'],
                                              end_time: snapshot.requireData.docs[index]
                                                  ['end_time'],
                                              name: snapshot.requireData.docs[index]
                                                  ['name'],
                                              org: snapshot.requireData.docs[index]
                                                  ['org'],
                                              photo: snapshot.requireData.docs[index]
                                                  ['photo'],
                                              place: snapshot.requireData.docs[index]
                                                  ['place'],
                                              start_time: snapshot.requireData.docs[index]
                                                  ['start_time'])));
                                },
                                child: Container(
                                  height: 200,
                                  width: 150,
                                  decoration: BoxDecoration(
                                      image: DecorationImage(
                                          image: NetworkImage(snapshot
                                              .requireData
                                              .docs[index]['photo']),
                                          fit: BoxFit.cover),
                                      borderRadius: BorderRadius.circular(20)),
                                  child: Align(
                                    alignment: Alignment.bottomLeft,
                                    child: Padding(
                                      padding: const EdgeInsets.only(
                                          bottom: 10, left: 10, right: 10),
                                      child: Text(
                                          snapshot.requireData.docs[index]
                                              ['name'],
                                          style: const TextStyle(
                                              color: Colors.white,
                                              fontSize: 15)),
                                    ),
                                  ),
                                ),
                              ),
                              const SizedBox(width: 10)
                            ],
                          );
                        }
                      },
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 10.0, right: 10),
                    child: Container(
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(20),
                          color: const Color.fromRGBO(240, 244, 253, 1)),
                      height: 80,
                      child: const Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              SizedBox(
                                width: 20,
                              ),
                              CircleAvatar(
                                radius: 25,
                                child: Icon(Icons.list_alt),
                              ),
                              SizedBox(
                                width: 10,
                              ),
                              
                              Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  
                                  Text(
                                    'Расписание',
                                    style: TextStyle(
                                      color: Colors.black,
                                    ),
                                  ),
                                  Text(
                                    'Составлен мастерами!',
                                    style: TextStyle(
                                      color: Colors.black,
                                    ),
                                  )
                                ],
                              ),
                            ],
                            
                          ),
                          
                          Row(
                            children: [
                              Icon(Icons.more_vert,
                                  color: Color.fromRGBO(158, 169, 188, 1)),
                              SizedBox(
                                width: 10,
                              )
                            ],
                          )
                        ],
                      ),
                    ),
                  ),
                  GestureDetector(
  onTap: () {
    // Add your gesture logic here
    print('Transparent gesture detected!');
  },
  child: Container(
    color: Color.fromARGB(230, 255, 0, 0),
    child: Text(
      'Tap me',
      style: TextStyle(
        color: Colors.black,
      ),
    ),
  ),
),
                  Padding(
                    padding: const EdgeInsets.only(left: 10.0, right: 10),
                    child: Container(
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(20),
                          color: const Color.fromRGBO(240, 244, 253, 1)),
                      height: 80,
                      child: const Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              SizedBox(
                                width: 20,
                              ),
                              CircleAvatar(
                                radius: 25,
                                child: Icon(Icons.list_alt),
                              ),
                              SizedBox(
                                width: 10,
                              ),
                              Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    'Меню столовой',
                                    style: TextStyle(
                                      color: Colors.black,
                                    ),
                                  ),
                                  Text(
                                    'Приятного аппетита!',
                                    style: TextStyle(
                                      color: Colors.black,
                                    ),
                                  )
                                ],
                              ),
                            ],
                          ),
                          Row(
                            children: [
                              Icon(Icons.more_vert,
                                  color: Color.fromRGBO(158, 169, 188, 1)),
                              SizedBox(
                                width: 10,
                              )
                            ],
                          )
                        ],
                      ),
                    ),
                  )
                ],
              );
            }),
      ),
    );
  }
}

class DescScreen extends StatefulWidget {
  String description;
  Timestamp end_time;
  String name;
  String org;
  String photo;
  String place;
  Timestamp start_time;
  DescScreen(
      {super.key,
      required this.description,
      required this.end_time,
      required this.name,
      required this.org,
      required this.photo,
      required this.place,
      required this.start_time});

  @override
  State<DescScreen> createState() => _DescScreenState();
}

class _DescScreenState extends State<DescScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ListView(children: [
        Padding(
          padding: const EdgeInsets.only(right: 20, left: 20),
          child: Column(
            children: [
              Container(
                height: 200,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  image: DecorationImage(
                      image: NetworkImage(widget.photo), fit: BoxFit.cover),
                ),
                child: Align(
                  alignment: Alignment.bottomRight,
                  child: Padding(
                    padding:
                        const EdgeInsets.only(bottom: 10, right: 10, left: 10),
                    child: Text(widget.name),
                  ),
                ),
              ),
              Text(widget.description),
              const SizedBox(
                height: 30,
              ),
              Container(
                height: 1,
                color: const Color.fromRGBO(205, 205, 205, 0.49),
              ),
              const SizedBox(
                height: 30,
              ),
              Container(
                height: 1,
                color: const Color.fromRGBO(205, 205, 205, 0.49),
              ),
              const SizedBox(
                height: 30,
              ),
              const Text('Информация'),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text('Место проведения'),
                  SizedBox(
                      width: 160, child: Center(child: Text(widget.place))),
                ],
              ),
              const SizedBox(
                height: 10,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text('Дата проведения'),
                  SizedBox(
                      width: 160,
                      child: Center(
                          child: Text(DateFormat('yyyy-MM-dd HH:mm')
                              .format(widget.start_time.toDate())
                              .toString()))),
                ],
              ),
              const SizedBox(
                height: 10,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text('Дата окончания'),
                  SizedBox(
                      width: 160,
                      child: Center(
                          child: Text(DateFormat('yyyy-MM-dd HH:mm')
                              .format(widget.end_time.toDate())
                              .toString()))),
                ],
              ),
              const SizedBox(
                height: 10,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text('Организаторы'),
                  SizedBox(width: 160, child: Center(child: Text(widget.org))),
                ],
              )
            ],
          ),
        ),
      ]),
    );
  }
}

im“itemCount:snapshot.requireData.size”,它说的是“StateError(Bad state:快照既没有数据也没有错误)”我不知道如何修复它
我试图重新连接firebase它不为我工作,也试图完全在另一个页面。这真的很令人困惑,只有收集不拥有

pbossiut

pbossiut1#

在使用snapshot之前,请确保它有数据

builder: (context, snapshot) {
    if(!snapshot.hasData) return CircularProgressIndicator();

    if(snapshot.hasData) return Column(
        /// Your implement
    )
}

希望可以帮到你!

相关问题