Question: Actually, I want to update a flag in Db using a service call(Delete method) once the user close the browser. I am able to detect browser close action using `onbeforeunload` and `onunload` events but async call doesn't work for me(sometimes in debugging mode it works fine but on higher env it doesn't work).

Then, I tried to make sync request but then I found that Chrome now disallows synchronous XHR during page dismissal when the page is being navigated away from or closed by the user. 
check link : <https://www.chromestatus.com/feature/4664843055398912>

I have tried new `XMLHttpRequest()` as sync, fetch api also `Navigator.sendBeacon()` but unfortunately nothing works for me.

Please suggest something which works because I have visited so many posts but nothing works for me.

Thanks in advance.

Answer: 
```
List<int> arr = [6, 12, 11, 18, 24,5,6,99,10,9];
    arr.sort((a, b) => a.compareTo(b));
    print(arr);
    print(Utils.getNextLargerNumber(8, arr));

```

and below is the logic:

```
static int getNextLargerNumber(int number, List<int> array)
  {
    for (var i = 0; i < array.length; i++) {
      if (number < array[i]) {
        return array[i];
      }
    }
    return -1;
  }

```
To get Closest Value of number

```
import 'dart:math';
import 'dart:collection';

void main(){
List<double> value = [1,4,6,3,7,9,12,34,12,-12,-91];
    print(value.getCloseValue(8));    // 7
    print(value.getCloseValue(6));    // 6
    print(value.getCloseValue(-11));  // -12

}

extension on List<num> {
  num getCloseValue(num x) {
    if (isEmpty) return 0;
    Map<num, num> values = {};
    forEach((e) {
      values[e] = (e - x).abs();
    });
  var sortedKeys = values.keys.toList(growable:false)
    ..sort((k1, k2) => values[k1]!.compareTo(values[k2]!));
    final sortedMap = LinkedHashMap
      .fromIterable(sortedKeys, key: (k) => k, value: (k) => values[k]);
    return sortedMap.keys.first;
  }
}

```