这是我用来从 Flutter 中的实时数据库中检索数据的代码,但它不起作用。
此行在 main.dart 中声明
DatabaseReference = newRequestRef=FirebaseDatabase.instance.reference().child("Ride Requests");
class PushNotifcationService {
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
Future initialize() async
{
firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
retrieveRideRequestInfo(getRideRequestId(message));
//Open the app and waits to see the ruquest
},
onLaunch: (Map<String, dynamic> message) async {
// will be launched after clicking notifications
retrieveRideRequestInfo(getRideRequestId(message));
},
onResume: (Map<String, dynamic> message) async {
//If driver is using any other APP , on resume will notify the the driver
retrieveRideRequestInfo(getRideRequestId(message));
},
);
}
Future<String> getToken() async {
String token = await firebaseMessaging.getToken();
print("This is Token::");
print(token);
driversRef.child(currentFirebaseUser.uid).child("token").set(token);
firebaseMessaging.subscribeToTopic("alldrivers");
firebaseMessaging.subscribeToTopic("allusers");
}
String getRideRequestId(Map<String, dynamic> message) {
String rideRequestId = "";
if (Platform.isAndroid) {
rideRequestId = message['data']['ride_request_id'];
}
else {
rideRequestId = message['ride_request_id'];
}
return rideRequestId;
}
void retrieveRideRequestInfo(String rideRequestId) {
newRequestRef.child(rideRequestId).once().then((DataSnapshot dataSnapShot) {
if(dataSnapShot.value != null) {
double pickUpLocationLat = double.parse(
dataSnapShot.value['pickup']['latitude']);
double pickUpLocationLng = double.parse(
dataSnapShot.value['pickup']['longitude']);
String pickUpAddress = dataSnapShot.value['pickup_address'].toString();
double dropOffLocationLat = double.parse(
dataSnapShot.value['dropOff']['latitude:']);
double dropOffLocationLng = double.parse(
dataSnapShot.value['dropOff']['longitude']);
String dropOffAddress = dataSnapShot.value['dropOff_address'].toString();
String paymentMethod = dataSnapShot.value['payment_method'].toString();
RideDetails rideDetails = RideDetails();
rideDetails.ride_request_id = rideRequestId;
rideDetails.pickup_address = pickUpAddress;
rideDetails.pickup_address = dropOffAddress;
rideDetails.pickup = LatLng(pickUpLocationLat, pickUpLocationLng);
rideDetails.dropOff = LatLng(dropOffLocationLat, dropOffLocationLng);
rideDetails.payment_method = paymentMethod;
print("Information::");
print(rideDetails.pickup_address);
print(rideDetails.dropOff_address);
}
});
}
}
现在,我无法在控制台中打印乘车详细信息。您能帮帮我吗?
我附上了一张图片来查看我的实时数据库在此处输入图片描述