Search code examples
flutterflutter-layoutflutter-dependenciesflutter-webflutter-animation

Saving Icon to Hive DB in Flutter


Can anyone help me. How to Save Icon in hive database in flutter. And to read it.

When I'm saving Icons it is giving error

unhandled exception: hiveerror: cannot write, unknown type: icon.

But when removing icon from hive database class and adapter it works fine.


Solution

  • I solved it by saving iconId instead of icon itself in the database.. In your model change the icon as

    final int iconCode;
    

    Now while saving icon, save it as

    iconCode = selectedIcon.codePoint,
    

    Now while displaying icon to the UI do something like this

    icon : Icon(IconData( selectedIcon.iconCode,fontFamily: 'MaterialIcons'));
    

    Also, feel free to assign appropriate fontFamily and also provide the fontPackage if you are using icons from other providers such as fontAwesomeIcon