在Flutter中根据DropdownButton的值使用if语句的方法是怎样的?

huangapple go评论58阅读模式
英文:

How to use if statements based on DropdownButton values in flutter

问题

You can solve the LateInitializationError by initializing the sex_value variable when declaring it. Here's an updated version of your code:

late String sex_value = ''; // Initialize it with an empty string

sex_controller(
  onSelected: (String value){
    sex_value = value;
  },
),

if(sex_value == 'male' || sex_value.isEmpty)
  male_weight_controller(
    onSelected: (String value){
      weight_value = value;
    }
  ),
if(sex_value == 'female')
  female_weight_controller(
    onSelected: (String value){
      weight_value = value;
    }
  ),

By initializing sex_value with an empty string, you prevent the LateInitializationError when accessing it later in your code.

英文:

I have three drop boxes
The first DropboxButton is called the sex_controller, which contains two values: male and female
The second DropboxButton is the male_weight_controller, which has values of -64, -70, -76, +82
The third DropboxButton is the female_weight_controller, which has values of -43, -48, -53, +58
It's in the form of a String

I want male_weight_controller if the value of sex_controller is male, and female_weight_controller if the value of sex_controller is female

late String sex_value;

sex_controller(
            onSelected: (String value){
               sex_value = value;
              },
            ),

if(sex_value == 'male' || sex_value.isEmpty)
           male_weight_controller(
             onSelected: (String value){
               weight_value = value;
             }
           ),
if(sex_value == 'female')
            female_weight_controller(
             onSelected: (String value){
                weight_value = value;
             }
            ),

error : ======== Exception caught by widgets library =======================================================
The following LateError was thrown building:
LateInitializationError: Local 'sex_value' has not been initialized.

How can you solve it?

答案1

得分: 0

错误消息提到了如果您的变量sex_value是一个LateInitializationError,这意味着它在使用之前尚未初始化。您可以通过确保在if条件中使用sex_value之前对其进行初始化来解决此问题。

String sex_value = 'male'; // 使用默认值初始化sex_value

sex_controller(
  onSelected: (String value){
    setState(() {
      sex_value = value;
    });
  },
)

if(sex_value == 'male' || sex_value.isEmpty)
  male_weight_controller(
    onSelected: (String value){
      setState(() {
        weight_value = value;
      });
    }
  );

if(sex_value == 'female')
  female_weight_controller(
    onSelected: (String value){
      setState(() {
        weight_value = value;
      });
    }
  );
英文:

The error message mentioned if your variable sex_value is a LateInitializationError, which means it hasn't been initialized before it is being used. you could fix this issue, by making sure that sex_value is initialized before it is used in the if conditions.

String sex_value = 'male'; // Initialize sex_value with a default value

sex_controller(
  onSelected: (String value){
    setState(() {
      sex_value = value;
    });
  },
),

if(sex_value == 'male' || sex_value.isEmpty)
  male_weight_controller(
    onSelected: (String value){
      setState(() {
        weight_value = value;
      });
    }
  ),
if(sex_value == 'female')
  female_weight_controller(
    onSelected: (String value){
      setState(() {
        weight_value = value;
      });
    }
  ),

huangapple
  • 本文由 发表于 2023年7月18日 12:54:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定