Search code examples
flutterdartdart-null-safety

Error : _TypeError (type 'Null' is not a subtype of type 'String')


Where do I need to fix this? It shows error : "_TypeError (type 'Null' is not a subtype of type 'String')" in readUserRideRequestInformation

class PushNotificationSystem
{
  FirebaseMessaging messaging = FirebaseMessaging.instance;

  Future initializeCloudMessaging(BuildContext context) async
  

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage? remoteMessage)
    {
      //display ride request information - user information who request a ride
      readUserRideRequestInformation(remoteMessage!.data["rideRequestId"], context);
    });
  }

function

import 'package:google_maps_flutter/google_maps_flutter.dart';

class UserRideRequestInformation
{
  LatLng? originLatLng;
  LatLng? destinationLatLng;
  String? originAddress;
  String? destinationAddress;
  String? rideRequestId;

  UserRideRequestInformation({
    this.originLatLng,
    this.destinationLatLng,
    this.originAddress,
    this.destinationAddress,
    this.rideRequestId,
  });
}

enter image description here

How do I fix it?


Solution

  • Instead of using bang !, you can do a null check, or use string format to pass null.

     final rideRequestId = remoteMessage?.data["rideRequestId"];
     if(rideRequestId != null){
        readUserRideRequestInformation(rideRequestId, context);
      }