For Example, there is a list containing double values:
[0.017, 0.068, 0.831, 1.034, 1.475, 1.593, 1.746, 2.017, 2.305, 2.695, 3.051, 3.339, 3.627, 3.763, 3.915, 3.966, 4.644, 4.949, 5.085, 5.203, 5.644, 6.136, 6.271, 6.542, 6.712, 7.085, 7.271, 7.661, 7.831, 8.085, 8.390, 8.915, 9.034, 9.492, 9.508, 9.898, 10.271, 10.610, 10.932, 11.169, 11.203, 11.729, 12.186, 12.288, 12.305, 12.644, 13.051, 13.407, 13.458, 13.729, 14.102, 14.525, 14.542, 14.831, 15.136, 15.559, 15.661, 16.169, 16.220, 16.763, 16.780, 17.119, 17.339, 17.390, 18.254, 18.322, 18.525, 18.746]
And there is double value:
0.905
How can we check which is the first number, that is nearer to 0.905 and greater than 0.905 is contained in the above list in flutter?
CodePudding user response:
Try this code
list.firstWhere(
(element) => element > 0.905,
orElse: () => 0,
)
CodePudding user response:
Lets call your list, _list
, then we can check it like this:
var result = _list.where((e) => e >= 0.905).toList();
print("result = $result");
CodePudding user response:
I'm using this to get the closest value from a list.
import 'package:collection/collection.dart';
double getClosest(double value, List<double> valueList) {
final List<double> diffList = valueList
.map((i) => (i - value).abs())
.toList();
return valueList.elementAt(
diffList.indexOf(diffList.min),
);
}
To get the first one which is equal or greater do this:
valueList.where((e) => e >= value).toList()
But keep in mind that the valueList
has to be ordered accordingly.
CodePudding user response:
You can use the where clause or a for. check :
final List<double> listDouble = [0.017, 0.068, 0.831, 0.905];
for (int i = 0; i < listDouble.length; i ) {
final double myVar = 0.905;
if (myVar == listDouble[i]){
print ('$myVar finded in position $i');
}
}
or :
final newDoubleList = listDouble.where((e) => e == myVar);
if (newDoubleList.isNotEmpty){
print('$myVar finded $newDoubleList');
}
CodePudding user response:
for simple way you can use following
yourDoubleList.where((element) => element > 0.905).toList()
advanced way
double myDoubleList = [.....]
double value = 0.905 ;
void handle(){
for(double val in myDoubleList ){
if(val>=value ){
print(val) // here you will have all the results that match
}
}
}
CodePudding user response:
List<double> samleList=[0.017, 0.068, 0.831, 1.034, 1.475, 1.593,
1.746, 2.017, 2.305, 2.695, 3.051, 3.339,
3.627, 3.763, 3.915, 3.966, 4.644, 4.949, 5.085, 5.203, 5.644, 6.136,
6.271.........];
double minDifference =0, givenNumber =0.905;
int findingIndex =0;
for(int i =0; i<samleList.length-1; i ){
if((samleList[i]>givenNumber) && (samleList[i] -
givenNumber<minDifference)){
minDifference ==samleList[i]-givenNumber;
findingIndex =i;
}
print(samleList[findingIndex]);
}
CodePudding user response:
Try the following code:
List list = [
0.017,
0.068,
0.831,
1.034,
1.475,
1.593,
1.746,
2.017,
2.305,
2.695,
3.051,
3.339,
3.627,
3.763,
3.915,
3.966,
4.644,
4.949,
5.085,
5.203,
5.644,
6.136,
6.271,
6.542,
6.712,
7.085,
7.271,
7.661,
7.831,
8.085,
8.390,
8.915,
9.034,
9.492,
9.508,
9.898,
10.271,
10.610,
10.932,
11.169,
11.203,
11.729,
12.186,
12.288,
12.305,
12.644,
13.051,
13.407,
13.458,
13.729,
14.102,
14.525,
14.542,
14.831,
15.136,
15.559,
15.661,
16.169,
16.220,
16.763,
16.780,
17.119,
17.339,
17.390,
18.254,
18.322,
18.525,
18.746,
];
final result = list.where((e) => e >= 0.905).toList()[0];
print(result); // 1.034