1
  1. 我正在尝试在 IconData 和 Icon Color 中添加 switch case 语句,当我使用它们时会抛出一些错误
  2. 必须先分配不可为空的局部变量“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);
                });
4

3 回答 3

0

在添加之前,有必要在那里声明 Icon 类。

尝试这个


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

于 2022-02-19T12:31:44.880 回答
0

你的 iconData 和 iconColor 都没有初始化。尝试

IconData iconData = Icons.add; // your icon
Color iconColor = Colors.blue; // your color
于 2022-02-19T08:06:42.557 回答
0

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

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);
                });
于 2022-02-19T08:24:14.713 回答