Ad

The Argument Type 'RemoteMessage' Can't Be Assigned To The Parameter Type 'Map'

- 1 answer

I had asked this question previously but could not get a solution. If I change the code String getRideRequestId(Map<String, dynamic> message) to String getRideRequestId(var message) the code it sends the message to the phone. It does not send the required rideDetails (Line 82,83 & 84).Iam looking to send the ride details as well. Please help me out here.

Here is the error log: Running Gradle task 'assembleDebug'... lib/Notifications/pushNotificationService.dart:17:48: Error: The argument type 'RemoteMessage' can't be assigned to the parameter type 'Map<String, dynamic>'.

  • 'RemoteMessage' is from 'package:firebase_messaging_platform_interface/src/remote_message.dart' ('/C:/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging_platform_interface-3.0.5/lib/src/remote_message.dart').
  • 'Map' is from 'dart:core'. retrieveRideRequestInfo(getRideRequestId(message)); ^ lib/Notifications/pushNotificationService.dart:22:50: Error: The argument type 'RemoteMessage' can't be assigned to the parameter type 'Map<String, dynamic>'.
  • 'RemoteMessage' is from 'package:firebase_messaging_platform_interface/src/remote_message.dart' ('/C:/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging_platform_interface-3.0.5/lib/src/remote_message.dart').
  • 'Map' is from 'dart:core'. retrieveRideRequestInfo(getRideRequestId(message));

Below is my code:

Main.dart

import 'package:driver/AllScreens/carInfoScreen.dart';
import 'package:driver/configMaps.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:driver/AllScreens/mainScreen.dart';
import 'package:driver/AllScreens/registrationScreen.dart';
import 'package:driver/DataHandler/appData.dart';

import 'AllScreens/loginScreen.dart';

Future<void> backgroundHandler(RemoteMessage message) async
{
  print(message.data.toString());
  print(message.notification!.title);
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseMessaging.onBackgroundMessage(backgroundHandler);
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  currentFirebaseUser = FirebaseAuth.instance.currentUser;
  runApp(MyApp());
}

DatabaseReference usersRef = FirebaseDatabase.instance.reference().child("users");
DatabaseReference driversRef = FirebaseDatabase.instance.reference().child("drivers");
DatabaseReference newRequestRef = FirebaseDatabase.instance.reference().child("Ride Requests");
DatabaseReference rideRequestRef = FirebaseDatabase.instance.reference().child("drivers").child(currentFirebaseUser!.uid).child("newRide");

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => AppData(),
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Driver',
        theme: ThemeData(
          primarySwatch: Colors.green,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        initialRoute: FirebaseAuth.instance.currentUser == null ? LoginScreen.idScreen : MainScreen.idScreen,
        routes: {
          RegistrationScreen.idScreen: (context) => RegistrationScreen(),
          LoginScreen.idScreen: (context) => LoginScreen(),
          MainScreen.idScreen: (context) => MainScreen(),
          CarInfoScreen.idScreen: (context) => CarInfoScreen(),
        },
      ),
    );
  }
}

pushNotificationService.dart

import 'package:driver/Models/rideDetails.dart';
import 'package:driver/configMaps.dart';
import 'package:driver/main.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'dart:io' show Platform;

class PushNotificationService {
  final FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;

  Future initialize(context) async {
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;

      retrieveRideRequestInfo(getRideRequestId(message));
    });

    FirebaseMessaging.onMessageOpenedApp.listen(
      (RemoteMessage message) {
        retrieveRideRequestInfo(getRideRequestId(message));
      },
    );
  }

  Future<String?> getToken() async {
    String? token = await firebaseMessaging.getToken();
    driversRef.child(currentFirebaseUser!.uid).child("token").set(token);

    print("Here is your token");
    print(token);

    firebaseMessaging.subscribeToTopic("alldrivers");
    firebaseMessaging.subscribeToTopic("allusers");
  }

  // String getRideRequestId(var message) {

  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'].toString());
        double pickupLocationLng =
            double.parse(dataSnapshot.value['pickup']['longitude'].toString());
        String pickupAddress = dataSnapshot.value['pickup_address'].toString();

        double dropoffLocationLat =
            double.parse(dataSnapshot.value['dropoff']['latitude'].toString());
        double dropoffLocationLng =
            double.parse(dataSnapshot.value['dropoff']['longitude'].toString());
        String dropoffAddress =
            dataSnapshot.value['dropoff_address'].toString();

        String paymentMethod = dataSnapshot.value['payment_method'].toString();

        String rider_name = dataSnapshot.value["rider_name"].toString();
        String rider_phone = dataSnapshot.value["rider_phone"].toString();

        RideDetails rideDetails = RideDetails();
        rideDetails.ride_request_id = rideRequestId;
        rideDetails.pickup_address = pickupAddress;
        rideDetails.dropoff_address = dropoffAddress;
        rideDetails.pickup = LatLng(pickupLocationLat, pickupLocationLng);
        rideDetails.dropoff = LatLng(dropoffLocationLat, dropoffLocationLng);
        rideDetails.payment_method = paymentMethod;
        rideDetails.rider_name = rider_name;
        rideDetails.rider_phone = rider_phone;

        print("Information :: ");
        print(rideDetails.pickup_address);
        print(rideDetails.dropoff_address);
      }
    });
  }
}

rideDetails.dart

import 'package:google_maps_flutter/google_maps_flutter.dart';

class RideDetails {
  String? pickup_address;
  String? dropoff_address;
  LatLng? pickup;
  LatLng? dropoff;
  String? ride_request_id;
  String? payment_method;
  String? rider_name;
  String? rider_phone;

  RideDetails({this.pickup_address,
    this.dropoff_address,
    this.pickup, this.dropoff,
    this.ride_request_id,
    this.payment_method,
    this.rider_name,
    this.rider_phone});
}
Ad

Answer

You are trying to send an object of class RemoteMessage in a function that accepts Map<String,dynamic>. To send the correct format of data use this reference

Convert your RemoteMessage to Map and then send it again. If you want to send data of notification, you can send like this:

retrieveRideRequestInfo(getRideRequestId(message.notification));

and then retrieve data in getRideRequestId(Map<String, dynamic> message) using:

 messageOfTitle=message["title"]
Ad
source: stackoverflow.com
Ad