在颤振中调用 getter 'length' 为 null

时间:2021-06-10 09:35:39

标签: flutter

我在我的主页上添加了三个 streamBuilder,所有 StreamBuilders 从云 Firestore 获取不同的数据,但我收到 'The getter 'length' was called on null in flutter' 。我试图在互联网上寻找解决方案,但都是徒劳的。我收到此错误:

════════ Exception caught by widgets library ═══════════════════════════════════
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was
StreamBuilder<List<Order>>
lib/…/home/home.dart:52

这是我的家。dart:

import 'package:flutter/material.dart';
import 'package:merza/models/orders.dart';
import 'package:merza/models/user.dart';
import 'package:merza/screens/home/items.dart';
import 'package:merza/screens/home/orders/delivered_order.dart';
import 'package:merza/screens/home/orders/order_status.dart';
import 'package:merza/screens/user/add_order.dart';
import 'package:merza/services/auth.dart';
import 'package:merza/services/databse.dart';
import 'package:merza/shared/grid.dart';
import 'package:intl/intl.dart';
import 'package:merza/shared/menus.dart';
import 'package:merza/shared/splash_screen.dart';
import 'package:provider/provider.dart';
import 'package:merza/screens/home/orders/orders_list.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

import 'orders/orders_tile.dart';
class Home extends StatelessWidget {
//Auth
  AuthService _auth = AuthService();

 //Database 
 Database _db = Database();
 
  @override
  Widget build(BuildContext context) {

   final user = Provider.of<User>(context);
   
  if(user.uid == null){
     Navigator.pushNamedAndRemoveUntil(context, "/home", (r) => false);

   }else{
   
    return StreamBuilder<UserData>(
      
      stream: Database(uid: user.uid).userData,
      builder: (context, snapshot) {
        
      if (snapshot.hasData) {
        UserData userData = snapshot.data;
        
      //Streambuilder for delivered orders
        return StreamBuilder<List<Order>>(
          stream: Database(receiver_nrc: userData.nrc, paramOne: 'payment_status', paramValue: 'unpaid').dataFetch,
          builder: (context, snapshot2) {
            String count_unpaid =  snapshot2.data.length.toString();
            if (snapshot2.hasData) {

            return StreamBuilder <List<Order>>(
              stream:  Database(receiver_nrc: userData.nrc, paramOne: 'order_status', paramValue: 'delivered').dataFetch,
              builder: (context, snapshot3) {
                String count_awaiting =  snapshot3.data.length.toString();
                if (snapshot3.hasData) {
                return StreamProvider<List<Order>>.value(
                  value: Database(receiver_nrc: userData.nrc).orders,
                    
                    child: Scaffold(
                    appBar: LoggedBar(title: 'Merza',),

                    body: SingleChildScrollView(
                            child: Column(
                        children: [

                          
                       
                       Container(),   // Required some widget in between to float AppBar

        Container(    // To take AppBar Size only
          
          child: AppBar(
            elevation: 0.0,
            backgroundColor: Colors.white,
            leading: IconButton(icon: Icon(Icons.home), color: Colors.green[900], onPressed: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) => Home()));
            },),
            primary: false,
            title: TextField(
                decoration: InputDecoration(
                    hintText: "Search",
                    border: InputBorder.none,
                    hintStyle: TextStyle(color: Colors.green[900]))),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.search, color: Colors.green[900]), onPressed: () {},),
             
            ],
          ),
        ),
                          Container(
                            color: Colors.green,
                            padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 20.0),
                         child: Text('A TRADING ZONE THAT PROMOTES SAFETY TRADING AND SECURE DIGITAL TRANSACTIONS', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14, color: Colors.white)),
                         
                         ), Container(
                            margin: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
                            child: Row(

                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [

                                Expanded(
                                  child: Card(
                                    
                                    child: Column(
                                      mainAxisSize: MainAxisSize.min,
                                      children: <Widget>[
                                         Container(
                                              width: 130,
                                              child: ListTile(

                                              title: Image.network('https://madvertadvertising.com/media/merza/images/seller.jpeg'),
                                              onTap: (){
                                           // Navigator.push(context, MaterialPageRoute(builder: (context) => ItemsPage()));
                                          },
                                          ),
                                            ),
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: <Widget>[
                                            
                                           
                                            const SizedBox(width: 8),
                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                                Expanded(
                                  child: Card(
                                    child: Column(
                                      mainAxisSize: MainAxisSize.min,
                                      children: <Widget>[
                                              Container(
                                              width: 130,
                                              child: ListTile(

                                              title: Image.network('https://madvertadvertising.com/media/merza/images/delivery.jpeg'),
                                              onTap: (){
                                             Navigator.push(context, MaterialPageRoute(builder: (context) => AwaitingOrdersList(nrc: userData.nrc, paramValueOne: 'order_status', paramValueTwo: 'delivered', barTitle: 'Delivered', isStaff: userData.is_staff.toString(), allowStaffPrivaledges: 'no',)));
                                          },
                                          ),
                                            ),
                                        
                                            const SizedBox(width: 8),
                                          
                                        
                                      ],
                                    ),
                                  ),
                                ),


                              ],
                            ),

                          ),


                          Container(
                            margin: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
                            child: Row(

                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [

                                Expanded(
                                  child: Card(
                                    
                                    child: Column(
                                      mainAxisSize: MainAxisSize.min,
                                      children: <Widget>[
                                         Container(
                                              width: 130,
                                              child: ListTile(

                                              title: Image.network('https://madvertadvertising.com/media/merza/images/items.jpeg'),
                                              onTap: (){
                                            Navigator.push(context, MaterialPageRoute(builder: (context) => ItemsPage()));
                                          },
                                          ),
                                            ),
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: <Widget>[
                                            
                                           
                                            const SizedBox(width: 8),
                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                                Expanded(
                                  child: Container(
                                    child: Card(
                                      child: Column(
                                        
                                        mainAxisSize: MainAxisSize.min,
                                        children: <Widget>[
                                            Container(
                                              width: 130,
                                              child: ListTile(

                                              title: Image.network('https://madvertadvertising.com/media/merza/images/payment.jpeg'),
                                          
                                          onTap: (){
                                             Navigator.push(context, MaterialPageRoute(builder: (context) => DeliverdordersList(nrc: userData.nrc, paramValueOne: 'payment_status', paramValueTwo: 'paid', barTitle: 'Paid orders',)));
                                          }
                                          ,
                                          ),
                                            ),
                                          
                                              const SizedBox(width: 8),
                                            
                                          
                                        ],
                                      ),
                                    ),
                                  ),
                                ),


                              ],
                            ),

                          ),
                          Text('My recent placed orders', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20)),
                        
                        Container(
                          child: OrdersList(),
                          ),
                       
                       SizedBox(height: 10.0),
                       
                        ],
                        
                        
                      ),
                    ),
                    



                    drawer: DrawerLogged()

                  ),
                );
                }else{
                  return LoadingScreenCustom(page:'home');
                }
              }
            );
            }else{
               return LoadingScreenCustom(page:'home');
            }
          }
        );
      }else{
        return LoadingScreenCustom(page:'home');
      }
      }
    );
  }
  }
}

我不知道在同一页面上添加多个流构建器是否是一个好习惯。

1 个答案:

答案 0 :(得分:0)

String count_unpaid = snapshot2.data.length.toString(); 行放在 if (snapshot2.hasData) 块内。

String count_awaiting = snapshot3.data.length.toString(); 行放在 if (snapshot3.hasData) 块内。