Flutter best way of storing FontAwesomeIcon in JSON?

Viewed 274

Trying to build my App apk Android Studio threw this:

This application cannot tree shake icons fonts. It has non-constant instances of IconData at the following locations:

I'm using the font_awesome_flutter plugin.

It's throwing a wobbler because I'm referencing FaIcons like so: FaIcon(IconDataSolid(presentLocation.descriptionButtonIcons[index]))

I'm handing IconDataSolid an int that I've decoded from JSON, JSON like this:
"descriptionButtonIcons": [62804, 62194],

I've essentially converted hex unicode Font Awesome identifiers to ints for JSON storage because I couldn't work out how to store hex in JSON.

I would have like to have stored the FaIcon more intuitively, e.g.

"descriptionButtonIcons": ["bicycle", "car"]

then used this to build the FaIcon widget:

FaIcon(FontAwesomeIcon.(presentLocation.descriptionButtonIcons[index]));

but that doesn't work, you can't call a class method with a variable.

This is the last hurdle! It's a small dataset, I've used JSON really just for practice, so what is the best, clearest way of storing specific FaIcon identifiers in JSON?

So say FaIcon(FontAwesomeIcons.bicycle) how to store that in JSON?
Or simply how otherwise to best overcome this hurdle.

Full problematic Widget building method, called from build():

  Widget buildLocationDescriptionWidgets(LocationDetails presentLocation) {
if(presentLocation.descriptionLinkUrls.isEmpty) return Text(presentLocation.description[0]);

List<Widget> colChildWidget = [];

for (int index = 0;
    index < presentLocation.description.length;
    index++) {
  colChildWidget.add(Text(presentLocation.description[index], textAlign: TextAlign.left,));
  if(index >= presentLocation.descriptionLinkUrls.length) continue; // no more buttons maybe more description Strings
  if (presentLocation.website[index] != null) {
    colChildWidget.add(Padding(
        padding: EdgeInsets.symmetric(vertical: 4),
        child: Align(alignment: Alignment.center, child: index >= presentLocation.descriptionButtonIcons.length
            ? ElevatedButton(
                child: Text(presentLocation.descriptionButtonText[index]),
                onPressed: () {
                  _launchURL(presentLocation.descriptionLinkUrls[index]);
                })
            : ElevatedButton(
                child: Row(mainAxisSize: MainAxisSize.min, children: [
                  Text(presentLocation.descriptionButtonText[index]),
                  SizedBox(width: 8),
                  FaIcon(IconDataSolid(
                      presentLocation.descriptionButtonIcons[index]), size: 16)
                ]),
                onPressed: () {
                  _launchURL(presentLocation.descriptionLinkUrls[index]);
                })))
    );
  }
}

return Column(children: colChildWidget, crossAxisAlignment: CrossAxisAlignment.start,);

}

1 Answers

OK well I used a lookup Map in the top of the Dart file, but I'd prefer a way to store Icons more directly in JSON.

  final Map<int, IconData> buttonIconMap = {
    62804 : FontAwesomeIcons.walking,
    63198 : FontAwesomeIcons.fistRaised,
    62373 : FontAwesomeIcons.gem,
    63189 : FontAwesomeIcons.dragon,
    62194 : FontAwesomeIcons.stopwatch,
    63208 : FontAwesomeIcons.hatWizard,
    61958 : FontAwesomeIcons.bicycle,
    61463 : FontAwesomeIcons.clock,
    61959 : FontAwesomeIcons.bus,
    63562 : FontAwesomeIcons.biking,
    62073 : FontAwesomeIcons.map,
  };
Related