非空局部变量'iconData'必须先赋值才能在flutter中使用?

The non-nullable local variable 'iconData' must be assigned before it can be used in flutter?

  1. 我正在尝试在 IconData 和 Icon Color 中添加 switch case 语句,当我使用它们时会抛出一些错误
  2. 不可为 null 的局部变量 'iconData' 必须先赋值才能使用。 尝试给它一个初始化表达式,或者确保它被分配到每个执行路径

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;

在主页我尝试使用 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);
                });

您的 iconData 和 iconColor 均未初始化。 尝试

IconData iconData = Icons.add; // your icon
Color iconColor = Colors.blue; // your color

我刚发现那个错误,非常感谢你们, 正确答案是

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);
                });

在添加之前,有必要在那里声明图标 class。

试试这个


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;

试试这个


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);
                });