Flutter枚举,在另一个文件上的静态变量无法设置。

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

Flutter Enum, static variable on another file cannot be set

问题

出现了以下错误:

无法将类型为'CalorieDensity(其中CalorieDensity在/Users/macbook/Projects/cookedcalories/lib/b.dart中定义)'的值分配给类型为'CalorieDensity(其中CalorieDensity在/Users/macbook/Projects/cookedcalories/lib/a.dart中定义)'的变量。
尝试更改变量的类型,或将右侧类型转换为'CalorieDensity(其中CalorieDensity在/Users/macbook/Projects/cookedcalories/lib/a.dart中定义)'

对于任何其他静态变量,我没有这种类型的问题。

英文:

Two files here: (as to simplify a.dart and b.dart, Classes A and B)

a.dart:

enum CalorieDensity { veryLow, low, medium, high, none }

class A extends StatefulWidget {
  const A({
    Key? key,
  }) : super(key: key);

  @override
  State<A> createState() => _AState();
}

class _AState extends State<A> {    
    
    static CalorieDensity calorieDensity = CalorieDensity.veryLow;
}

b.dart:

enum CalorieDensity { veryLow, low, medium, high, none }
    
class B extends StatefulWidget {
  const B({
    Key? key,
  }) : super(key: key);

  @override
  State<B> createState() => _BState();
}

class _BState extends State<B> {

   void aSimpleFunction(){

       A.calorieDensity = CalorieDensity.veryLow;
    
    }
}

I get this error:

A value of type 'CalorieDensity (where CalorieDensity is defined in /Users/macbook/Projects/cookedcalories/lib/b.dart)' can't be assigned to a variable of type 'CalorieDensity (where CalorieDensity is defined in /Users/macbook/Projects/cookedcalories/lib/a.dart)'.
Try changing the type of the variable, or casting the right-hand type to 'CalorieDensity (where CalorieDensity is defined in /Users/macbook/Projects/cookedcalories/lib/a.dart)'

I have not such type of issues for any other static variable.

答案1

得分: 3

我认为这是因为你在a.dart文件和b.dart文件中都定义了枚举CalorieDensity两次。

enum CalorieDensity { veryLow, low, medium, high, none }

只需将上述行写一次,如果需要在其他地方使用CalorieDensity,则导入该文件,而不是创建一个新的。

英文:

I think it's because you're defining enum CalorieDensity twice once in a.dart file and once in b.dart file.

enum CalorieDensity { veryLow, low, medium, high, none }

Just write the above line once and import that file if you need to use CalorieDensity elsewhere instead of creating a new one.

答案2

得分: 1

问题:

错误发生是因为您试图将一个在一个文件中定义的枚举分配给另一个文件中定义的枚举类型的变量。

尽管它们具有相同的值,但这两个枚举是不同的。

所有枚举都会自动扩展Enum类,这意味着在定义枚举时会创建一个新的Enum对象

您可以通过在两个文件中定义CalorieDensity枚举并比较它们的相等性来证明这一点。结果将是false。

请参见下面的示例:

// calorie_density_1.dart

enum CalorieDensity { veryLow, low, medium, high, none }
// calorie_density_2.dart

enum CalorieDensity { veryLow, low, medium, high, none }
// main.dart

import 'enum_1.dart' as calorieDensity1;
import 'enum_2.dart' as calorieDensity2;

void main() {
  print(calorieDensity1.CalorieDensity == calorieDensity2.CalorieDensity); // 返回false
}

解决方案:

解决方案是在一个文件中定义枚举,并在需要使用它的任何地方导入它。

这确保您在应用程序的操作中使用相同的对象。

我建议您将其定义在与屏幕文件不同的文件中,因为您在多个屏幕中使用它。

英文:

Problem:

The error is occurring because you are trying to assign an enum defined in one file to a variable of a type of enum defined in another file.

Those two enums are different even though they have the same values.

All enums automatically extend the Enum class and this means that a new Enum object is created when you define an Enum.

You can demonstrate this by defining the CalorieDensity enum in two files and comparing them for equality. The result will be false.

See below:

// calorie_density_1.dart

enum CalorieDensity { veryLow, low, medium, high, none }
// calorie_density_2.dart

enum CalorieDensity { veryLow, low, medium, high, none }
// main.dart

import 'enum_1.dart' as calorieDensity1;
import 'enum_2.dart' as calorieDensity2;

void main() {
  print(calorieDensity1.CalorieDensity == calorieDensity2.CalorieDensity); // Returns false
}

Solution:

The solution is to define the enum in one file and import it for use wherever you want to use it.

This ensures you are using the same object for your app's operations.

I'll suggest you define it in a file different from your screen files since you use it in multiple screens.

答案3

得分: 0

已解决,感谢Imtiaz Ayon(部分地)。

仅仅从B文件中移除枚举是不够的(但这是方法)。
我还需要将枚举重命名为CalorieDensityEnum。

我认为这是因为在使用calorieDensity作为CalorieDensity枚举的变量时出现了问题(奇怪,Dart是一种大小写敏感的语言)。

英文:

Solved thanks to Imtiaz Ayon (partially).

By removing the enum from B file is not enough (but this is the way).
I also needed to rename the enum to CalorieDensityEnum.

I think because something went wrong by using calorieDensity as a variable per CalorieDensity enum (strange, Dart is a case sensitive language).

huangapple
  • 本文由 发表于 2023年5月28日 02:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348416.html
匿名

发表评论

匿名网友

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

确定