我如何等待一个值

问题描述 投票:0回答:0

如何等待从另一个类返回的值?

主类按钮

                  alignment: Alignment.centerRight,
                  

                  onPressed: () async {
                    print(selectedYear);

                    selectedYear = await int.tryParse(
                        SetYear(context: context, selectedYear: selectedYear!)
                            .setYear()
                            .toString());
                    print(selectedYear);
                  },
                  icon: Icon(Icons.calendar_month_outlined)),

第一张是2023,第二张是null。 第二次打印后,显示另一类“支票”的打印 我怎么能等到从 SetYear 类中选择年份?

SetYear 类


class SetYear {
  BuildContext context;
  int selectedYear = 0;

  SetYear({
    required this.context,
    required this.selectedYear,
  });

  Future setYear() async {
    DateTime _selectedDate = DateFormat("yyyy").parse("$selectedYear");
   await showDialog(
      context: context,
      builder: (BuildContext context) {
        print("Check");
        return AlertDialog(
          title: Text("Auswahl Jahr"),
          content: Container(
            // Need to use container to add size constraint.
            width: 300,
            height: 300,
            child: YearPicker(
              firstDate: DateTime(2021),
              lastDate: DateTime(2050),
              initialDate: DateTime.now(),
              selectedDate: _selectedDate,
              onChanged: (DateTime newYear) {
                if (newYear != _selectedDate) {
                  selectedYear =
                      int.tryParse("${DateFormat("yyyy").format(newYear)}") ??
                          2022;

                  ;
                }

                // close the dialog when year is selected.
                Navigator.pop(context);
              },
            ),
          ),
        );
      },
    );
    print("From inside");
    print(selectedYear);
    return Future.value(selectedYear);
  }
}

这是打印订单

扑动:2023 扑动:空 扑动:检查 扑动:从里面 颤振:2022

flutter dart asynchronous
© www.soinside.com 2019 - 2024. All rights reserved.