1
  1. I'm trying to add the switch case statement in IconData and Icon Color when i use they throw some errors
  2. The non-nullable local variable 'iconData' must be assigned before it can be used. Try giving it an initializer expression, or ensure that it's assigned on every execution path

TodoAddPage.dart

 class TodoCardPage extends StatefulWidget {
      const TodoCardPage({
        Key? key,
        required this.title,
        required this.iconData,
        required this.iconColor,
      }) : super(key: key);
      final String title;
      final IconData iconData;
      final Color iconColor;

In home page I try to use homePage.dart

ListView.builder(
                itemCount: (snapshot.data! as QuerySnapshot).docs.length,
                itemBuilder: (context, index) {
                  IconData iconData;
                  Color iconColor;
                  Map<String, dynamic> document =
                      (snapshot.data! as QuerySnapshot).docs[index].data()
                          as Map<String, dynamic>;

                  return TodoCardPage(
                      title: document["title"] == null
                          ? "Hey There"
                          : document["title"],
                      iconData: iconData,
                      iconColor: iconColor,
                      time: "10 PM",
                      value: true,
                      iconBgColor: Colors.white);
                });
  • you are not initializing `iconData`. – John Joe Feb 19 '22 at 08:01
  • i use it in the switch case like switch(document["category"]){ case "Work": iconData: Icon(Icons.add),iconColor:Colors.red;break; defaul: iconData: Icon(Icons.plus),iconColor:Colors.blue} –  Feb 19 '22 at 08:14

3 Answers3

0

Both your iconData & iconColor are not initialized. try

IconData iconData = Icons.add; // your icon
Color iconColor = Colors.blue; // your color
Edie Kamau
  • 230
  • 1
  • 5
  • i use it in the switch case –  Feb 19 '22 at 08:10
  • like switch(document["category"]){ case "Work": iconData: Icon(Icons.add),iconColor:Colors.red;break; defaul: iconData: Icon(Icons.plus),iconColor:Colors.blue} –  Feb 19 '22 at 08:11
  • but you are defining them in the Listview builder. I think you should define then above the switch statement – Edie Kamau Feb 19 '22 at 08:12
  • try `late IconData iconData;`. Then switch.... – Edie Kamau Feb 19 '22 at 08:14
  • i tried it won't work –  Feb 19 '22 at 08:15
  • You can create a function to get them. ` IconData _getIconData(String icon){ switch..... return _iconData}. ` then on your builder use ` iconData: _getIconData(document[icon]')` – Edie Kamau Feb 19 '22 at 08:18
0

I just found that error thank you so much guys, the right answer is

ListView.builder(
                itemCount: (snapshot.data! as QuerySnapshot).docs.length,
                itemBuilder: (context, index) {
                  IconData iconData;
                  Color iconColor;
                  Map<String, dynamic> document =
                      (snapshot.data! as QuerySnapshot).docs[index].data()
                          as Map<String, dynamic>;
                  switch (document["category"]) {
                    case "Work":
                      iconData = (Icons.breakfast_dining);
                      iconColor = Colors.red;
                      break;
                    default:
                      iconData = (Icons.add);
                      iconColor = Color.fromARGB(255, 56, 46, 196);
                  }
                  return TodoCardPage(
                      title: document["title"] == null
                          ? "Hey There"
                          : document["title"],
                      iconData: iconData,
                      iconColor: iconColor,
                      time: "10 PM",
                      value: true,
                      iconBgColor: Colors.white);
                });
0

Before adding it would be necessary to declare there Icon class.

Try this


class TodoCardPage extends StatefulWidget {
      const TodoCardPage({
        Key? key,
        required this.title,
        required this.iconData,
        required this.iconColor,
      }) : super(key: key);
      final String title;
      final Icon myIcon;
      final Color iconColor;

Try this


ListView.builder(
                itemCount: (snapshot.data! as QuerySnapshot).docs.length,
                itemBuilder: (context, index) {
                  IconData iconData;
                  Color iconColor;
                  Map<String, dynamic> document =
                      (snapshot.data! as QuerySnapshot).docs[index].data()
                          as Map<String, dynamic>;
                  switch (document["category"]) {
                    case "Work":
                      iconData = (Icons.breakfast_dining);
                      iconColor = Colors.red;
                      break;
                    default:
                      iconData = (Icons.add);
                      iconColor = Color.fromARGB(255, 56, 46, 196);
                  }
                  return TodoCardPage(
                      title: document["title"] == null
                          ? "Hey There"
                          : document["title"],
                      iconData: myIcon(iconData),// this line
                      iconColor: iconColor,
                      time: "10 PM",
                      value: true,
                      iconBgColor: Colors.white);
                });