x86
x86

Reputation: 519

The argument type 'Set<Marker>?' can't be assigned to the parameter type 'Set<Marker>'

When i try to assign set of markers to markers argument in GoogleMap it gives me this error...enter image description here The argument type 'Set?' can't be assigned to the parameter type 'Set'.

Code Sample

body: GoogleMap(
    initialCameraPosition: CameraPosition(
      target: LatLng(
        widget.initialLocation!.latitude!,
        widget.initialLocation!.longitude!,
      ),
      zoom: 16,
    ),
    onTap: widget.isSelecting! ? _selectLocation : null,
    
    markers: _pickedLocation == null
        ? null
        : {
            Marker(
              markerId: MarkerId('m1'),
              position: _pickedLocation!,
            ),
          },
  ),

Upvotes: 1

Views: 2236

Answers (3)

Randy Yeki
Randy Yeki

Reputation: 1

Try to set the marker value this way

markers: _pickedLocation == null
            ? <Marker>[].toSet()
            : [
          Marker(
            markerId: MarkerId('m1'),
            position: _pickedLocation!,
          ),
        ].toSet(),

Upvotes: 0

Mark
Mark

Reputation: 380

You can return an empty set which will satisfy the non-null requirement of the markers: property

markers: (_pickedLocation == null && !widget.isSelecting)
        ? {}
        : [
            Marker(
              markerId: MarkerId('m1'),
              position: _pickedLocation ??
                  LatLng(
                    widget.initialLocation.latitude!,
                    widget.initialLocation.longitude!,
                  ),
            ),
          ].toSet(),

Upvotes: 0

mohamed nasser
mohamed nasser

Reputation: 51

markers: (_pickedLocation == null && !widget.isSelecting)
        ? <Marker>[].toSet()
        : [
            Marker(
              markerId: MarkerId('m1'),
              position: _pickedLocation ??
                  LatLng(
                    widget.initialLocation.latitude!,
                    widget.initialLocation.longitude!,
                  ),
            ),
          ].toSet(),

Upvotes: 3

Related Questions