sfMap primaryValueMapper cause exception

Hi,

I am trying to to bind map to display data from api dynamically. When use primaryValueMapper, I get following error message. Could you advice how to solve this issue?


Exception has occurred.

_AssertionError ('package:syncfusion_flutter_maps/src/layer/shape_layer.dart': Failed assertion: line 168 pos 16: '(primaryValueMapper != null && dataCount > 0) ||

            primaryValueMapper == null': is not true.)


Image_1615_1693376596481


class MapModel {

  final String NAME;

  final double? count;

  String? color;

  MapModel({required this.NAME, this.count, this.color});

}


MapModel getFirstDataPfMix(dynamic row) {

  final MapModel mapModel = MapModel(

    NAME: row[1],

    count: double.tryParse(row[2]) ?? 0,

  );


  final String color;

  if (row[2] == 0) {

    color = "#daf2b9";

  } else if (double.tryParse(row[2])! >= 1 && double.tryParse(row[2])! <= 100) {

    color = "#54de03";

  } else if (double.tryParse(row[2])! >= 101 &&

      double.tryParse(row[2])! <= 200) {

    color = "#97d806";

  } else {

    color = "#d2d009";

  }


  mapModel.color = color;

  return mapModel;

}

Best regards
Sao



1 Reply

HK Hariharasudhan Kanagaraj Syncfusion Team August 30, 2023 07:33 AM UTC

Hi Sao,


We would like to inform you that the stated assertion throws an error when the primaryValueMapper property is provided and the value of the dataCount property is not greater than zero. By default, the value of the dataCount property is zero. To avoid the mentioned assertion error, we suggest using the dataCount property and assigning the required value to it as shown in the code snippet below.


class _MyHomePageState extends State<MyHomePage> {

  late MapShapeSource _shapeSource;

  late List<MapModel> _models;

 

  @override

  void initState() {

    _models = [

      MapModel(continents: 'Asia', count: 1, color: Colors.red),

      MapModel(continents: 'Europe', count: 2, color: Colors.blue),

      MapModel(continents: 'North America', count: 3, color: Colors.green),

      MapModel(continents: 'South America', count: 4, color: Colors.yellow),

      MapModel(continents: 'Australia', count: 5, color: Colors.purple),

    ];

    _shapeSource = MapShapeSource.asset(

      'assets/world_map.json',

      shapeDataField: 'continent',

      dataCount: _models.length,

      primaryValueMapper: (int index) {

        return _models[index].continents!;

      },

      shapeColorValueMapper: (index) {

        return _models[index].color;

      },

    );

    super.initState();

  }

 

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      body: Padding(

        padding: const EdgeInsets.all(10),

        child: SfMaps(

          layers: [

            MapShapeLayer(

              source: _shapeSource,

            ),

          ],

        ),

      ),

    );

  }

}

 

class MapModel {

  final String? continents;

  final num? count;

  final Color? color;

 

  MapModel({

    this.continents,

    this.count,

    this.color,

  });

}


Snapshot:


If you have further queries, please get back to us.


Regards,
Hari Hara Sudhan. K.


Loader.
Up arrow icon