Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
good evening sir, ladies, I have an error that I can't resolve despite the tips I got on the net. please guide me.
I am told this: The method 'data' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target ('!').
the error is in Map _productMap = productSnap.data.data();
    if (productSnap.connectionState ==
                            ConnectionState.done) {
                          Map _productMap = productSnap.data.data();

                          return Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(12.0),
                            ),
                            // height: 350.0,
                            margin: EdgeInsets.symmetric(
                              upright: 12.0,
                              horizontal: 24.0,
                            ),
                            /*child: Text("Name: ${document['name']}"),*/
                            child:Container(
                              child: Text("${_productMap['name']}"),
                            ),
                          );
                        }




Here is the full code:
//import 'dart:html';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:djasse_application/ecran/product_page.dart';
import 'package:djasse_application/services/firbase_services.dart';
import 'package:djasse_application/widget/custon_action_bar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';

import '../constant.dart';

class cartpage extends StatefulWidget {
  const cartPage({Key? key}): super(key: key);

  @override
  State<pagePanier> createState() => _pagePanierState();
}

class _pageCartState extends State<pageCart> {
  /*final CollectionReference _productRef =
      FirebaseFirestore.instance.collection("Users");*/

  FirebaseServices _firebaseServices = FirebaseServices();

  @override
  widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: [
        FutureBuilder<QuerySnapshot>(
          future: _firebaseServices.userRef
              .doc(_firebaseServices.getUserId())
              .collection("Basket")
              .get(),
          builder: (context, snapshot) {
            if (snapshot. hasError) {
              return Scaffold(
                body: Center(
                  child: Text("Error: ${snapshot.error}"),
                ),
              );
            }
            //collection Data ready to be displayed
            if (snapshot. connectionState == ConnectionState. done) {
              //Display data in a list view
              return ListView(
                padding: EdgeInsets.only(
                  high: 108.0,
                  bottom: 12.0,
                ),
                children: snapshot.data!.docs.map((document) {
                  return GestureDetector(
                    /*Click to display image or image info in another page */
                    onTap: () {
                      Navigator.push(
                        background,
                        MaterialPageRoute(
                          builder: (context) => ProductPage(
                            productId: document.id,
                          ),
                        ),
                      );
                    },
                    child: FutureBuilder(
                      future:
                          _firebaseServices.productRef.doc(document.id).get(),
                      builder: (context, productSnap) {
                        /*Map _productMap = productSnap.data.data();*/
                        if (productSnap. hasError) {
                          return Container(
                            child: Center(
                              child: Text("${productSnap.error}"),
                            ),
                          );
                        }
                        if (productSnap.connectionState ==
                            ConnectionState.done) {
                          final _productMap = productSnap.data.data();

                          return Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(12.0),
                            ),
                            // height: 350.0,
                            margin: EdgeInsets.symmetric(
                              upright: 12.0,
                              horizontal: 24.0,
                            ),
                            /*child: Text("Name: ${document['name']}"),*/
                            child:Container(
                              child: Text("${_productMap['name']}"),
                            ),
                          );
                        }
                      },
                    ),
                  );
                }).toList(),
              );
            }

            //Loading state
            return const Scaffold(
              body: Center(
                child: CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(
                      Color.fromARGB(255, 243, 159, 80)),
                ),
              ),
            );
          },
        ),
        CustomActionBar(
            title: "Basket",
            hasBackArrow: true,
            hasTitle: true,
            hasBackground: false)
      ],
    ));
  }
}


What I have tried:

final

Posted
Updated 3-Oct-22 23:04pm

Follow the very clear instructions from the error message:
Quote:
Try making the call conditional (using '?.') or adding a null check to the target ('!').

So either:
Flutter
Map? _productMap = productSnap.data?.data();
Or, if you're certain the data will never be null:
Flutter
Map _productMap = productSnap.data!.data();
 
Share this answer
 
Comments
ago2486 4-Oct-22 5:16am    
Hello sir, I have just tried two solutions but it does not work.
Richard Deeming 4-Oct-22 5:17am    
"It does not work" tells us precisely nothing. Provide a clear and precise description of the problem, including the full details of any errors.
Richard Deeming 4-Oct-22 5:25am    
So based on your deleted comment, you have completely ignored the suggestion in the error message, and completely ignored the code in my solution. If you're not going to change the code, then you're not going to fix the error!
ago2486 4-Oct-22 6:46am    
here is the error: The method 'data' isn't defined for the type 'Object'.
Try correcting the name to the name of an existing method, or defining a method named 'data'.

child: FutureBuilder(
future:
_firebaseServices.productRef.doc(document.id).get(),
builder: (context, productSnap) {
/*Map _productMap = productSnap.data.data();*/
if (productSnap.hasError) {
return Container(
child: Center(
child: Text("${productSnap.error}"),
),
);
}
if (productSnap.connectionState ==
ConnectionState.done) {
Map? _productMap = productSnap.data?.data();

return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
),
//height: 350.0,
margin: EdgeInsets.symmetric(
vertical: 12.0,
horizontal: 24.0,
),
/*child: Text("Nom: ${document['nom']}"),*/
child: Container(
child: Text("${_productMap['nom']}"),
),
);
}
},
),
Richard Deeming 4-Oct-22 6:48am    
So once again: read the error message. You are trying to access a property (data) on a variable defined as the Object type. That type doesn't have a property called data.
Quote:
here's what the error says: The 'data' method cannot be invoked unconditionally because the receiver may be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target

and here is the full code:

//import 'dart:html';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:djasse_application/ecran/page_produit.dart';
import 'package:djasse_application/services/firbase_services.dart';
import 'package:djasse_application/widget/custon_action_bar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';

import '../constant.dart';

class pagePanier extends StatefulWidget {
  const pagePanier({Key? key}) : super(key: key);

  @override
  State<pagePanier> createState() => _pagePanierState();
}

class _pagePanierState extends State<pagePanier> {
  /*final CollectionReference _productRef =
      FirebaseFirestore.instance.collection("Users");*/

  FirebaseServices _firebaseServices = FirebaseServices();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: [
        FutureBuilder<QuerySnapshot>(
          future: _firebaseServices.userRef
              .doc(_firebaseServices.getUserId())
              .collection("Panier")
              .get(),
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Scaffold(
                body: Center(
                  child: Text("Erreur: ${snapshot.error}"),
                ),
              );
            }
            //collection Données prêtes à être affichées
            if (snapshot.connectionState == ConnectionState.done) {
              //Afficher les données dans une vue de liste
              return ListView(
                padding: EdgeInsets.only(
                  top: 108.0,
                  bottom: 12.0,
                ),
                children: snapshot.data!.docs.map((document) {
                  return GestureDetector(
                    /*Click pour afficher l'image ou les info de l'image dans une autre page */
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => PageProduit(
                            produitId: document.id,
                          ),
                        ),
                      );
                    },
                    child: FutureBuilder(
                      future:
                          _firebaseServices.productRef.doc(document.id).get(),
                      builder: (context, productSnap) {
                        /*Map _productMap = productSnap.data.data();*/
                        if (productSnap.hasError) {
                          return Container(
                            child: Center(
                              child: Text("${productSnap.error}"),
                            ),
                          );
                        }
                        if (productSnap.connectionState ==
                            ConnectionState.done) {
                          Map _productMap = productSnap.data.data();

                          return Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(12.0),
                            ),
                            //height: 350.0,
                            margin: EdgeInsets.symmetric(
                              vertical: 12.0,
                              horizontal: 24.0,
                            ),
                            /*child: Text("Nom: ${document['nom']}"),*/
                            child: Container(
                              child: Text("${_productMap['nom']}"),
                            ),
                          );
                        }
                      },
                    ),
                  );
                }).toList(),
              );
            }

            //État de chargement
            return const Scaffold(
              body: Center(
                child: CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(
                      Color.fromARGB(255, 243, 159, 80)),
                ),
              ),
            );
          },
        ),
        CustomActionBar(
            title: "Panier",
            hasBackArrow: true,
            hasTitle: true,
            hasBackground: false)
      ],
    ));
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900