改变一个自定义小部件在Flutter中使用if语句。

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

To change a custom widget in a plutter using an if statement

问题

我想要在ageValue的值小于13时显示a_page,小于15时显示b_page,小于17时显示c_page。

final ageValue = 0;
final age;
if(ageValue < 17){
   age = const c_page;
} else if(ageValue < 15){
   age = const b_page;
} else if(ageValue < 13){
   age = const a_page;
} else {

}

如果之后使用age变量,您将会得到以下错误:

错误 The final variable 'age' can't be read because it's potentially unassigned at this point. (Documentation) Ensure that it is assigned on necessary execution paths.

如果您根据ageValue提供一个if语句,age会发生变化,我原以为使用age会允许我在没有任何主要问题的情况下获取和使用自定义小部件。但这是错误的,我找不到解决方法。

英文:

I want to show a_page if the ageValue value is less than 13, b_page if it is less than 15, and c_page if it is less than 17

final ageValue = 0;
final age;
if(ageValue &lt;= 17){
   age = const c_page;
} else if(ageValue &lt;= 15{
   age = const b_page;
} else if(ageValue &lt;= 13{
   age = const a_page;
} else {

}

If you use age after that, you will get the following error

error The final variable &#39;age&#39; can&#39;t be read because it&#39;s potentially unassigned at this point. (Documentation) Ensure that it is assigned on necessary execution paths.

If you give me an if statement according to the ageValue, the age changes, and I thought that using age would allow me to take and use custom widgets without any major problems. But it was wrong and I couldn't find a way.

答案1

得分: 1

在Dart语言中,当您定义一个final变量时,您应该在编译时初始化它,但您可以将其标记为late,late意味着您将在首次调用之前在运行时初始化它,请尝试以下代码:

final ageValue = 0;
late final Widget age;
if (ageValue <= 17) {
  age = const c_page;
} else if (ageValue <= 15) {
  age = const b_page;
} else if (ageValue <= 13) {
  age = const a_page;
} else {

}
英文:

in dart language when you define a final variable, you should initialize it at compile time, but you can late it, late means that you will initialize it at runtime before first call, try this code:

final ageValue = 0;
late final Widget age;
if(ageValue &lt;= 17){
age = const c_page;
} else if(ageValue &lt;= 15{
age = const b_page;
} else if(ageValue &lt;= 13{
age = const a_page;
} else {

}

答案2

得分: 0

问题在于您试图向尚未分配的 age 变量添加值,您可以将 age 变量声明为 late final,以确保编译器知道这个变量将在将来分配,或者移除 final 并为其添加一些初始值。

并且通过查看您的代码,我可以看到您的条件也是错误的;看起来我们拿您的代码并假设 ageValue 为 12,然后它进入第一个条件,即 ageValue &lt;= 17,这是 true,12 < 17 是 true,所以应该显示 a_page,这是错误的,它应该显示 c_page

所以应该像下面这样:

final ageValue = 0;
late final age;

if (ageValue &lt;= 13) {
  age = c_page;
} else if (ageValue &lt;= 15) {
  age = b_page;
} else if (ageValue &lt;= 17) {
  age = a_page;
} else {

}
英文:

The issue is you're tying to add value in age variable which is not assigned yet,

You can either make the age variable as late final so we are assuring the compiler that this variable will be assigned in the future.
Or Remove the final and add some initialise value to it.

And going through your code i can see your conditions are also wrong; See we take your code and assume ageValue is 12 then it goes to the first condition which is ageValue &lt;= 17 which is true, 12 < 17 is true so the a_page will be shown which wrong, it should show the c_page.

So It should be like below:

  final ageValue = 0;
  late final age;

  if (ageValue &lt;= 13) {
    age = c_page;
  } else if (ageValue &lt;= 15) {
    age = b_page;
  } else if (ageValue &lt;= 17) {
    age = a_page;
  } else {
    
  }

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

发表评论

匿名网友

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

确定