英文:
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;
});
}
),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论