1

我有一个网格视图构建器,但我的问题是,当数据库中没有数据时,它仍然返回一个我设置为包含数据的容器,如果有数据,它不会根据数据库中的数据复制容器,它仍然只返回一个容器,我什至将它设置为创建一个小部件,如果它找不到数据,它会说没有数据,但如果我从集合中删除所有数据,它仍然没有显示

这是我的代码

import 'package:flutter/material.dart';
import 'package:flutter_bounce/flutter_bounce.dart';
import 'package:swipe_to/swipe_to.dart';
import 'package:page_indicator/page_indicator.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class HomeScreen extends StatefulWidget {
  @override
  Home createState() => Home( );
}

class Home extends State<HomeScreen> {

    String user_id = "2467046743";

   checkCategories() { 
    var docSnapshot =  FirebaseFirestore.instance
        .collection('categories').where('id',isEqualTo:user_id.toString()).snapshots();
 print ("found data on cloudstore "+docSnapshot.toString());

    return docSnapshot;

  }


  bool step1 = true;
  bool step2 = false;
   
  @override
  void initState() {
   
    super.initState();
   
  }
  

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff0B0B0B),
      body: 
           Stack(
  children: <Widget>[
                 SafeArea(child:
                 
                        /* PageIndicatorContainer( 
                      
                      
                      align: IndicatorAlign.bottom,
                      length: 2,
                      indicatorSpace: 5.0, 
                      padding: EdgeInsets.only(bottom: MediaQuery.of(context).size.height*6.5/100 + 7,),
                      indicatorColor: Colors.white, 
                      indicatorSelectorColor: Color(0xff00bdf4),
                      shape: IndicatorShape.circle(size: 6),
                      
                                    child:*/ PageView(
                                        children: <Widget>[

                 Container(  color:  Color(0xff0B0B0B), 



              child: 
              
              
              
              
              
              Column(
                
                mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center, 
                children: <Widget>[

                  Center(child:Container( margin: EdgeInsets.only(top:10), width: MediaQuery.of(context).size.width*90/100, height: 50,
                          decoration: BoxDecoration(
                            
        

      borderRadius: BorderRadius.circular(5),
    
                               
                color: Color(0xff323336), 
              
                          ),
                  
                  child: TextField( style: TextStyle(
      color: Color(0xfffA3A3AB),
      fontFamily: 'Montserrat',
      fontSize: 20,
      fontWeight: FontWeight.w500),
  decoration: InputDecoration( contentPadding: const EdgeInsets.only(top:13,left:0),
    prefixIcon: Padding(
                padding: const EdgeInsets.all(10.0),
                    child:Transform.scale(
  scale: 1.2,
  child:  Image.asset(
                  'assets/images/search.png',
                  width: 10,
                  height: 10,
                  fit: BoxFit.contain,color: Color(0xff90939A),
                )),
              ), suffixIcon: Padding(
                padding: const EdgeInsets.all(10.0),
                child:Transform.scale(
  scale: 1.0,
  child:   Image.asset(
                  'assets/images/bar_code_scanner.png',
                  width: 20, 
                  height: 20, color: Color(0xff989EA4),
                  fit: BoxFit.contain,
                ),
              )),
    border: InputBorder.none,
    hintText: 'Search', hintStyle: TextStyle(
      color: Color(0xffBABABD),
      fontSize: 19,
      fontWeight: FontWeight.w500)
  ),
) )),
Container( height:MediaQuery.of(context).size.height *90/100,
 decoration: BoxDecoration(
   
  ),

    child: Stack( 
  children: <Widget>[


    Container(color: Color(0xff0B0B0B), 
    
    child:  SingleChildScrollView(

            child: ConstrainedBox(
              constraints: BoxConstraints(
                minHeight: MediaQuery.of(context).size.height,
              ),
         
        child: Column(
                
                mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center, 
                children: <Widget>[
                  
                  StreamBuilder(
                      stream: checkCategories(),
                      builder: (context, snapshot) {
                        if (snapshot.hasData) {
                          if (snapshot.data.documents.isEmpty) {

                           print("noooooooooooooooooooooooooooooooooooooooooooooooooo");

                                return Text("No Data Found");



                          }


                          else if(snapshot.data.documents.isNotEmpty){


                 print("yessssssssssssssssssssssssssssssssssssssssssssssssssss");


              return   GridView.builder(
                
    shrinkWrap: true,
  itemCount: snapshot.data.documents.length,
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
    childAspectRatio: 1.7,
      crossAxisCount:2),
  itemBuilder: (BuildContext context, int index) {

    DocumentSnapshot thesnapshot = snapshot.data.docs[index];

    return  Container( margin:EdgeInsets.only(left:25,right:25, top:20),height: 200,decoration: BoxDecoration(
                       
               color: Color(0xff1e82d2),
               
               
                borderRadius: BorderRadius.circular(5),));



  },
);
 
    
    
    
                          }
    
    
                        }
                        return Container();
                        
                        })
4

0 回答 0