Home > database >  Icons not appearing
Icons not appearing

Time:01-03

I am using a class in a listview listtile to display the Icons but for some reason the icons aren't appearing

CircleAvatar(
  backgroundColor: Color(forumdata.getfcolor(index)),
  child: Icon(
    forumdata.getIcon(index),
    color: Color(forumdata.geticolor(index)),
  ),
),

this is the forumdata class and the forum class I have no idea if using icondata was the correct move or not


class ForumData {
  List<Forum> _forumData =[
    Forum(0xff517C7E, 0xff3ABEC5, Icons.announcement,"Welcome New Members & FAQs", "cat", "Welcome new Members & FAQs on how to use this online social support community", "8 aug 2022, 14:39", 123),
    Forum(0xff5C9A9E, 0xff03F2FF, Icons.favorite_outline,"I Care For My Partner With Dementia", "cat", "Welcome new Members & FAQs on how to use this online social support community", "28 sep 2022, 22:11", 13),
    Forum(0xff3ABEC5, 0xff00DEEB, Icons.home_outlined,"I Care For My Parent With Dementia", "cat", "Welcome new Members & FAQs on how to usethis online social support community", "12 jan 2022, 19:39", 20),
  ];

  IconData getIcon(int index){
    return _forumData[index].icon;
  }


}

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Forum{
  IconData icon = Icons.add;
  String title = "";
  String user = "";
  String desc = "";
  String datetime = DateTime.now().toString();
  int noofposts = 0;
  int color = 0;
  int icolor = 0;

  Forum(int fcolor, int icolor, IconData ficon, String forumtitle, String userforum, String forumdesc, String datetimeforum, int nop){
    color = fcolor;
    icolor = icolor;
    icon = ficon;
    title = forumtitle;
    user = userforum;
    desc = forumdesc;
    datetime = datetimeforum;
    noofposts = nop;
  }

}

CodePudding user response:

It's because there's a variable name overlap, when you assign "icolor"

icolor = icolor;

you're actually referencing the same variable twice, so the value remains 0 unchanged, rename your local variable to something else, or you can say

this.icolor = icolor;

or use the Dart sytanx for constructors which is much cleaner and to avoid mistakes like this one

class Forum {

  IconData icon;
  String title;
  String user;
  String desc;
  String datetime;
  int noofposts;
  int color;
  int icolor;

  Forum(
    this.color,
    this.icolor,
    this.icon,
    this.title,
    this.user,
    this.desc,
    this.noofposts,
    String? pDatetime,
  ) : datetime = pDatetime ?? DateTime.now().toString();
}

CodePudding user response:

Just clean the project with

flutter clean

in terminal and rebuild the project.

  • Related