Home > Software design >  Why I got : The final variable 'd' can only be set once. Try making 'd' non-fina
Why I got : The final variable 'd' can only be set once. Try making 'd' non-fina

Time:10-19

I'm on mixing two lists using flutter I declare a variable as final and when I tried to reuse it I got : The final variable 'd' can only be set once.Try making 'd' non-final. And I want it to be as final. this is my code :

final d = [];
    chunks.forEach((chunk) {
      final c = chunk as List<int>;
      final list4 = [(c[c.length - 2] ^ c[c.length - 1])];
      d = c   list4;
    });

    print('List With CRC : $d ');
    return d;
  }

Any suggestions please.

CodePudding user response:

Try this :

late final List d;
        chunks.forEach((chunk) {
          final c = chunk as List<int>;
          final list4 = [(c[c.length - 2] ^ c[c.length - 1])];
          d = c   list4;
        });
    
        print('List With CRC : $d ');
        return d;
      }

CodePudding user response:

In flutter, final variables can only be set once. In your code, you change it's value multiple times when you do this in a loop:

d = c   list4;

So, you either remove the final keyword, or limit yourself to set it's value only once.

  • Related