6

I want to use my image from assets as an icon, and then I build my lists, but there are some errors that occurred when trying to add Image.asset as a class to the icon as a variable with IconData as data type, instead of using Icons as a class.

Any idea to fix this problem?

Thank you.

class Menu {
  const Menu({this.icon, this.title});

  final IconData icon;
  final String title;
}

const List<Menu> menus = const <Menu>[
  const Menu(title: 'menu_icon_1', icon: Image.asset('assets/menu/1-1.png')),
],
error: The argument type 'Image' can't be assigned to the parameter type 'IconData'. (argument_type_not_assignable at [sinergi] lib\home.dart:12)
Tharindu Lakshan
  • 3,995
  • 6
  • 24
  • 44
dexdim
  • 185
  • 1
  • 4
  • 11

2 Answers2

8

please use ImageIcon

class Menu {
  const Menu({this.icon, this.title});

  final ImageIcon icon;
  final String title;
}

const List<Menu> menus = const <Menu>[
  const Menu(title: 'menu_icon_1', icon: ImageIcon(AssetImage('assets/menu/1-1.png'))),
];
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
-2

if you want default icon

class Menu {
    const Menu({this.icon, this.title});

    final IconData icon;
    final String title;
}

const List<Menu> menus = const <Menu>[
    const Menu(title: 'Trending', icon: Icons.trending_up),
    const Menu(title: 'Favorite', icon: Icons.favorite),
    const Menu(title: 'Search', icon: Icons.search),
    const Menu(title: 'Category', icon: Icons.category),
    const Menu(title: 'Settings', icon: Icons.settings),
];