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

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

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:

  1. late String sex_value = ''; // Initialize it with an empty string
  2. sex_controller(
  3. onSelected: (String value){
  4. sex_value = value;
  5. },
  6. ),
  7. if(sex_value == 'male' || sex_value.isEmpty)
  8. male_weight_controller(
  9. onSelected: (String value){
  10. weight_value = value;
  11. }
  12. ),
  13. if(sex_value == 'female')
  14. female_weight_controller(
  15. onSelected: (String value){
  16. weight_value = value;
  17. }
  18. ),

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

  1. late String sex_value;
  2. sex_controller(
  3. onSelected: (String value){
  4. sex_value = value;
  5. },
  6. ),
  1. if(sex_value == 'male' || sex_value.isEmpty)
  2. male_weight_controller(
  3. onSelected: (String value){
  4. weight_value = value;
  5. }
  6. ),
  7. if(sex_value == 'female')
  8. female_weight_controller(
  9. onSelected: (String value){
  10. weight_value = value;
  11. }
  12. ),

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之前对其进行初始化来解决此问题。

  1. String sex_value = 'male'; // 使用默认值初始化sex_value
  2. sex_controller(
  3. onSelected: (String value){
  4. setState(() {
  5. sex_value = value;
  6. });
  7. },
  8. )
  9. if(sex_value == 'male' || sex_value.isEmpty)
  10. male_weight_controller(
  11. onSelected: (String value){
  12. setState(() {
  13. weight_value = value;
  14. });
  15. }
  16. );
  17. if(sex_value == 'female')
  18. female_weight_controller(
  19. onSelected: (String value){
  20. setState(() {
  21. weight_value = value;
  22. });
  23. }
  24. );
英文:

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.

  1. String sex_value = 'male'; // Initialize sex_value with a default value
  2. sex_controller(
  3. onSelected: (String value){
  4. setState(() {
  5. sex_value = value;
  6. });
  7. },
  8. ),
  9. if(sex_value == 'male' || sex_value.isEmpty)
  10. male_weight_controller(
  11. onSelected: (String value){
  12. setState(() {
  13. weight_value = value;
  14. });
  15. }
  16. ),
  17. if(sex_value == 'female')
  18. female_weight_controller(
  19. onSelected: (String value){
  20. setState(() {
  21. weight_value = value;
  22. });
  23. }
  24. ),

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:

确定