Home > Blockchain >  Unexpected null value
Unexpected null value

Time:05-19

This is my homepage.dart. The error here said that there is an unexpected null value. I've been trying to debug this for the day but i still could not manage to find it, how to solve this problem? When i try to run the code. It won't shows the data from the 'Parking' which is inside the firestore database. The data was indeed there but it wont show inside this page. Can anyone help me with this ?

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'addparking.dart';

class AdminHomePage extends StatefulWidget {
  const AdminHomePage({Key? key}) : super(key: key);
  @override
  State<AdminHomePage> createState() => _AdminHomePageState();
}



class _AdminHomePageState extends State<AdminHomePage> {
  CollectionReference ref = FirebaseFirestore.instance.collection('Parking');
  //DELETE
  Future<void> _deleteProduct(String productId) async {

    await ref.doc(productId).delete();

    // Show a snackbar
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
        content: Text('You have successfully deleted a product')));
  }
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Colors.lightBlueAccent,
      appBar: AppBar(
          backgroundColor: Color(0xFF121212),
          elevation: 0.0,
          title: const Text('Admin Page',
            style:TextStyle(
              color: Color(0xFFFFFFFF),
              fontWeight: FontWeight.bold,
            ),
          ),
          centerTitle:true,
      ),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance.collection('Parking').snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
          if(snapshot.hasData){
            return ListView.builder(
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (context,index){
                  final DocumentSnapshot documentSnapshot = (snapshot.data!.docs[index]);
                    return Card(
                      margin: const EdgeInsets.all(10),
                      child: ListTile(
                        title: Text(documentSnapshot['level']),
                        subtitle: Text(documentSnapshot['parking']),
                        trailing: Row(
                          children: [
                            IconButton(
                              icon: const Icon(Icons.edit),
                                onPressed: (){}
                            ),
                            IconButton(
                                icon: const Icon(Icons.delete),
                                onPressed: () => _deleteProduct(documentSnapshot.id),
                            ),
                          ],
                        ),
                      ),
                    );



            });
          }
            return const Center(
              child: CircularProgressIndicator(),
            );
        },
      ),
  
      drawer:  const NavigationDrawer(),
    );

  }
}
  class NavigationDrawer extends StatelessWidget {
  const NavigationDrawer({Key? key}) : super(key: key);


  @override
  Widget build(BuildContext context) => Drawer(
    child: SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children:  <Widget> [
          buildHeader(context),
          buildMenuItems(context),
        ],
      ),
    ),
  );

  Widget buildHeader (BuildContext context) => Container(
    color: Colors.amber,
    child: InkWell(
      onTap: (){},
      child: Container(
        padding: EdgeInsets.only(
        top: 24    MediaQuery.of(context).padding.top,
        bottom: 24,
      ),
        child: Column(
          children: const [
            CircleAvatar(
              radius: 40,
              backgroundImage: NetworkImage(
                'https://www.shutterstock.com/image-vector/people-icon-vector-illustration-flat-design-405042562'
              ),
            ),
            SizedBox(height: 12),
            Text(
              'Admin'
            )
          ],
        ),
      ),
    ),
  );

  Widget buildMenuItems (BuildContext context) => Container(
    padding: const EdgeInsets.all(24),
    child: Wrap(
      children: [
        ListTile(
          leading: const Icon(Icons.home_outlined),
          title: const Text('Home'),
          onTap:(){}
        ),
        const Divider(color: Colors.black54,),
        ListTile(
            leading: const Icon(Icons.home_outlined),
            title: const Text('Add Parking'),
            onTap:(){
              Navigator.pop(context);
              Navigator.of(context).push(MaterialPageRoute(builder: (context) => AddParking(),));
            }
        ),
        const Divider(color: Colors.black54,),
        ListTile(
            leading: const Icon(Icons.home_outlined),
            title: const Text('Delete Parking'),
            onTap:(){}
        ),
        const Divider(color: Colors.black54,),
        ListTile(
            leading: const Icon(Icons.home_outlined),
            title: const Text('Update Parking'),
            onTap:(){}
        ),
        const Divider(color: Colors.black54,),
        ListTile(
            leading: const Icon(Icons.home_outlined),
            title: const Text('Report Feedback'),
            onTap:(){}
        ),
        const Divider(color: Colors.black54,),
        ListTile(
            leading: const Icon(Icons.home_outlined),
            title: const Text('Payment Setup'),
            onTap:(){}
        ),
      ],
    ),
  );

}

CodePudding user response:

try 1- documentSnapshot?['level'] 2- documentSnapshot?['parking']

CodePudding user response:

It's really hard to track down the error without the stacktrace but you can try to change the following lines:

  1. Text(documentSnapshot['level'] ?? "")
  2. Text(documentSnapshot['parking'] ?? "")

Text() requires a string as a parameter that isn't null and documentSnapshot['level'] returns an nullable value because the key level might not exist. Same for parking.

  • Related