skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

    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).


  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search