简体   繁体   中英

The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'

in a Dart class, I am trying to retrieve the position of the rider from the **Firebase Realtime ** Database. but keep getting the error.

Note: I am following a 2-year-old tutorial but all my SDK is up to date. AS a result, I might not know the new format. I would appreciate any help on this.

The Code:

void FetchRideInfo(String rideID) {
    DatabaseReference rideRef =
        FirebaseDatabase.instance.ref().child('rideRequest/$rideID');

    rideRef.once().then((DatabaseEvent databaseEvent) {
      if (databaseEvent.snapshot.value != null) {
        double pickupLat = double.parse(
            databaseEvent.snapshot.value['location']['latitude'].toString()); /error here
      }
    });
  }

The error on ['location'] is

The operator '[]' isn't defined for the type 'Object'.
Try defining the operator '[]'.

错误

databaseEvent is a property with type object. An indexer cannot be called on an object. In order to use it, you need to first cast it.

You need to use the cast operator as . The following should work:

double pickupLat = double.parse((databaseEvent.snapshot.value as List)['location']['latitude'].toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM