Flutter DropDownButton选择调用setState上的所有API

pepwfjgg  于 2023-03-13  发布在  Flutter
关注(0)|答案(1)|浏览(139)

对于我的应用程序,我有下拉菜单来选择年份。当我选择一个值时,整个应用程序开始调用各种API,我猜这是由于setState。

DropdownButton(
            style: const TextStyle(
              color: Colors.blue,
            ),
            icon: const Icon(Icons.keyboard_arrow_down),
            iconSize: 12,
            iconEnabledColor: Colors.red,
            hint: const Text(
              "MM",
              style: TextStyle(
                color: Colors.blueAccent,
              ),
            ),
            items: getYears().map<DropdownMenuItem<String>>((String prod) {
              return DropdownMenuItem(
                value: prod,
                child: Text(prod),
              );
            }).toList(),
            onChanged: (String? newValue) {
              setState(() {
                year = newValue!;
              });

              print(year);
            },
            value: year,
          ),

当我改变这个下拉菜单的值时,前面下拉菜单的API调用被调用,即使它们没有被触及。有什么建议吗?如何避免对其他下拉菜单使用setState进行不必要的api调用?

toe95027

toe950271#

listOfTransactionType = await TransactionTypeFromServer.data.response.data
  .map((e) => e.type)
  .toList();

通知监听器();

final List<DropdownMenuItem<String>> transactionTypedropdownmenuItems =
    listOfTransactionType
        .map((String value) =>
            DropdownMenuItem<String>(value: value, child: Text(value))).toList();

相关问题