Home > Net >  Unable to read node changes when the Stream will fire a new event. firebase database v9
Unable to read node changes when the Stream will fire a new event. firebase database v9

Time:04-25

Works fine with firebase database v8 and not on v9. From what I read from other users -Firebase v9, they moved from using dynamic to Object? . I can't implement the changes in my code. Could you please take a look? More of this on this thread

     StreamSubscription<DatabaseEvent>? rideStreamSubscription;
      rideStreamSubscription = rideRequestRef!.onValue.listen((DatabaseEvent event) async {
       
        if (event.snapshot.value["car_details"] != null) {
          carDetailsDriver = (event.snapshot.value["car_details"].toString());
          Provider.of<AppData>(context, listen: false)
              .carDetailsDriverMainScreen(carDetailsDriver);

          if (event.snapshot.value!["driver_id"] != null) {
            rideReqAcceptDriver =
                (event.snapshot.value["driver_id"].toString());
            print("rideReqAcceptDriver $rideReqAcceptDriver");
          }
        }

errors are on event.snapshot.value["car_details"]

The method '[]' 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 ('!')

and on event.snapshot.value!["driver_id"]

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

CodePudding user response:

Try this.

StreamSubscription<DatabaseEvent>? rideStreamSubscription;
  rideStreamSubscription = rideRequestRef!.onValue.listen((DatabaseEvent event) async {
    dynamic data = event.snapshot.value;
    if (data["car_details"] != null) {
      carDetailsDriver = (data["car_details"].toString());
      Provider.of<AppData>(context, listen: false)
          .carDetailsDriverMainScreen(carDetailsDriver);

      if (data["driver_id"] != null) {
        rideReqAcceptDriver =
            (data["driver_id"].toString());
        print("rideReqAcceptDriver $rideReqAcceptDriver");
      }
    }
  • Related