Home > Back-end >  How to get list index values from an unlist variable?
How to get list index values from an unlist variable?

Time:05-15

var _first=<double> [108,105,90.833,87.7,88.6];
 var _answers=<double>[];
 for (var i=0; i<_first.length;i  ){
   _answers.add(_first[i]);
   double _cosa = Angle.degrees(_first[i]).cos;
print (_cosa);
**double a=_cosa[0];
double b=_cosa[1];
double c=_cosa[2];
double d=_cosa[3];
double e=_cosa[4];**

There are 5 values in the print of the _cosa variable, and I want to get all 5 values in 5 different variables like I typed the code, but this throws an error. So, can anyone tell me the correct code?

CodePudding user response:

I think creating variables like you want is not very good idea. However,I recommend you to use Map to get any data(or value) within Map by 'key'. Here is example:

 List<double> _first=<double> [108,105,90.833,87.7,88.6];
  Map<String,double> _answers={};
 for (var i=0; i<_first.length;i  ){
   _answers['b${i}']=_first[i]; // you can  change 'b${i}' key as you want
 }
  
   double _cosa = Angle.degrees(_answers['a1']).cos;
   print (_cosa);

CodePudding user response:

So you seem to be mistaken on what _cosa is. _cosa is not a list of values, it is a value in itself, meaning it has only one value inside itself.

You loop through each value in _first and do the following:

double _cosa = Angle.degrees(_first[i]).cos;

There are 2 things wrong with this piece of code:

  1. You assign to _cosa directly:

That is to say, every time you loop you are saying _cosa is equal to the current angle's sin wave. Meaning that by the time the loop is done, the _cosa variable will only contain the last item's sin wave, not a list of all. You should declare the _cosa variable as a list just like you did for the previous items:

var _cosa = <double>[0,0,0,0,0];
_cosa[i] = Angle.degrees(_first[i]).cos;

A smarter way to do this, so that you don't have to declare _cosa with a bunch of ugly ceroes, is to use the add method:

var _cosa = <double>[];
_cosa.add(Angle.degrees(_first[i]).cos);

This solves the first issue, but it still won't work, because:

  1. You declare cosa inside the loop itself:

if you run the code above in the loop itself, every time the loop runs (5 times) this line also runs:

var _cosa = <double>[];

which will do two things, because you declare the variable inside the loop, you will get an error for using it outside the loop. Remember that every variable declared inside a loop (or inside {} in general) gets destroyed as soon as you leave said loop.

The second thing it does is assign make _cosa be empty every loop, effectively deleting all of the data you store in it.

To fix this, move the declaration of _cosa to before the loop:

var _first=<double> [108,105,90.833,87.7,88.6];
 var _answers=<double>[];
 var _cosa = <double>[];
 for (var i=0; i<_first.length;i  ) {
   _answers.add(_first[i]);
   _cosa.add(Angle.degrees(_first[i]).cos);
}

print (_cosa);
double a=_cosa[0];
double b=_cosa[1];
double c=_cosa[2];
double d=_cosa[3];
double e=_cosa[4];
  • Related