Home > Enterprise >  The operator '[]' isn't defined for the type 'Object'. Try defining the ope
The operator '[]' isn't defined for the type 'Object'. Try defining the ope

Time:03-11

I'am trying to fetch some details from my firebase database, the below is my code. Here i'am getting the error The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'. in the line double.parse(dataSnapshot.value!['pickUp']['latitude'].toString());. What is the issue??

DatabaseReference newRequestsRef =
    FirebaseDatabase.instance.ref().child('Ride Requests');

 void retrieveRideRequestInfo(String rideRequestId, BuildContext context) {
    newRequestsRef.child(rideRequestId).once().then((value) {
      var dataSnapshot = value.snapshot;
     
      if (dataSnapshot.value != null) {
        double pickUpLocationLat =
            double.parse(dataSnapshot.value!['pickUp']['latitude'].toString());
      }
    });
  }

CodePudding user response:

void retrieveRideRequestInfo(
      String rideRequestId, BuildContext context) async {
    newRequestsRef.child(rideRequestId).once().then((value) {
      var dataSnapshot = value.snapshot;
      final map = dataSnapshot.value as Map<dynamic, dynamic>;
      if (dataSnapshot.value != null) {
        double pickUpLocationLat =
            double.parse(map['pickUp']['latitude'].toString());
      }
    });
  }

I Found the solution just make it map final map = dataSnapshot.value as Map<dynamic, dynamic>;

CodePudding user response:

If you're sure there is a nested Map in pickup maybe you could try:

final Map<String, dynamic> pickup = dataSnapshot.value!['pickUp'];
double pickUpLocationLat = double.parse(pickup['latitude'].toString());

Also if you could add a screenshot of your document model or the debugger value of snapshot.value it could help

  • Related