flutter:错误,dart:266未捕获(在承诺中)错误:应为“int”类型的值,但在setState下拉列表中获得了“String”类型的值

s2j5cfk0  于 2022-11-30  发布在  Flutter
关注(0)|答案(2)|浏览(105)

我目前正在创建一个下拉列表,它的值应该是动态的,取决于我将要在不同的小部件中使用的所选值。这是我当前的下拉列表有状态小部件:
周期_模态.dart

extension StringExtension on String {
    String capitalize() {
      return "${this[0].toUpperCase()}${this.substring(1).toLowerCase()}";
    }
}

class DropDown1 extends StatefulWidget {
  DropDown1({super.key});

  @override
  State<DropDown1> createState() => _DropDown1State();
}

class _DropDown1State extends State<DropDown1> {
  String? selectedMonth;
  String? selectedYear;
  
  @override
  Widget build(BuildContext context) {
    print("Selection month = ${Selection.currMonth}");
    return Row(
      children: [
        DropdownButton(
      // isExpanded: true,
          hint: Text("Pilih Bulan"),
          underline: Container(
            height: 2,
            color: Colors.black,
          ),
          icon: Visibility(visible: false, child: Icon(Icons.arrow_downward)),
          items: months
              .map((item) => DropdownMenuItem<String>(
                    value: item,
                    child: Text(
                      item,
                      style: const TextStyle(
                        fontSize: 14,
                        color: Colors.black,
                      ),
                      overflow: TextOverflow.ellipsis,
                    ),
                  ))
              .toList(),
          value: Selection.currMonth.capitalize().isEmpty?null:Selection.currMonth.capitalize(),
          onChanged: (value) {
            setState(() {
              selectedMonth = value as String;
              Selection.currMonth = value as String;
              Selection.nextMonth = value as String;
            });
          },
        ),

        SizedBox(
          width: 50,
        ),

        DropdownButton(
          underline: Container(
            height: 2,
            color: Colors.black,
          ),
          icon: Visibility(visible: false, child: Icon(Icons.arrow_downward)),
          items: years
              .map((item) => DropdownMenuItem<String>(
                    value: item,
                    child: Text(
                      item,
                      style: const TextStyle(
                        fontSize: 14,
                        color: Colors.black,
                      ),
                      overflow: TextOverflow.ellipsis,
                    ),
                  ))
              .toList(),
          hint: Text("Pilih Tahun"),
          value: Selection.currYear == -1 ? null : Selection.currYear.toString(),
          onChanged: (value) {
            setState(() {
              // selectedYear = value as String;
            
              Selection.currYear = value as int;
              print("value = ${value} selection currYear = ${Selection.currYear}");
              print("Selection.currYear = ${Selection.currYear}");
              Selection.nextYear = value as int;
              print("Selection.nextYear = ${Selection.nextYear}");
            });
          })
      ],
    );
  }
}

home_page.dart(整个文件的一部分)

class Selection{
  static int _currYear = 0;
  static String _currMonth = "";
  static int _nextYear = 0;
  static String _nextMonth = "";

  static int get currYear => _currYear;
  static String get currMonth => _currMonth;
  static int get nextYear => _nextYear;
  static String get nextMonth => _nextMonth;

  static set currYear(int value) => _currYear = value;
  static set currMonth(String value) => _currMonth = value;
  static set nextYear(int value) => _nextYear = value;
  static set nextMonth(String value) => _nextMonth = value;
}

在我做了一个小的调试之后,我有一个暗示,在periodic_model.dart中的这部分代码有问题

onChanged: (value) {
            setState(() {
              // selectedYear = value as String;
            
              Selection.currYear = value as int;
              print("value = ${value} selection currYear = ${Selection.currYear}");
              print("Selection.currYear = ${Selection.currYear}");
              Selection.nextYear = value as int;
              print("Selection.nextYear = ${Selection.nextYear}");
            });
          })

如果我把print("value = ${value} selection currYear = ${Selection.currYear}");写在Selection.currYear = value as int;的上面,它会在我得到错误之前成功地打印出来。但是如果我按照我在代码段中的方式写的话--我得到了错误而没有打印出来,因此我假设Selection.currYear = value as int;中有什么地方出错了,尽管我不是100%肯定。
我该如何解决这个问题?

//编辑

这是years的列表

final List<String> years = [
  '2022',
  '2021',
  '2020',
  '2019',
  '2018',
  '2017',
  '2016',
  '2015',
  '2014',
  '2013',
  '2012',
  '2011',
  '2010',
  '2009',
];

//编辑2:这是放置在home_page.dart中的选择的类

class Selection{

  static List<List<Map<String,String>>> dataDummy = dummy;
  static int _currYear = 0;
  static String _currMonth = "";
  static int _nextYear = 0;
  static String _nextMonth = "";

  static int get currYear => _currYear;
  static String get currMonth => _currMonth;
  static int get nextYear => _nextYear;
  static String get nextMonth => _nextMonth;

  static set currYear(int value) => _currYear = value;
  static set currMonth(String value) => _currMonth = value;
  static set nextYear(int value) => _nextYear = value;
  static set nextMonth(String value) => _nextMonth = value;
  
}
kqqjbcuj

kqqjbcuj1#

我猜years数据就是List<String>

int.parse(value);

Selection.currYear = value as int;之前插入上一行
希望工作顺利。编码快乐🧑‍💻

lp0sw83n

lp0sw83n2#

我设法找到了答案,但我不完全确定我的方法是否正确,但对于这个特定的问题,它设法解决它。
setState()线上

Selection.currYear = value as int;

我将其更改为

Selection.currYear = int.parse(value??"0");

我也是为了

Selection.nextYear = value as int;

相关问题