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: 
Just filter the `List` only for the values higher or equal to your number and get the lowest value:

```dart
var n = 8; // Number to match
var l = [3, 7, 12, 19]; // List of values

var greater = l.where((e) => e >= n).toList()..sort(); //List of the greater values

print(greater.first); // Print the first value. -> 12

```
[Mattia](https://stackoverflow.com/a/60551837/11003497)'s answer is already good enough. (although the list cant have the length 0 and it might be not as efficient, as you have a `where()` as well as `sort()` in there). Here is a different approach, that solves those **concerns**:

Nearest-Larger value
--------------------

```dart
final nearestLarger = list.isEmpty ? null : list.reduce(
      (a, b) => (a-target).abs() < (b -target).abs() ? a : b);

```

Nearest-Smaller value
---------------------

```dart
final nearestSmaller = list.isEmpty ? null : list.reduce(
      (a, b) => (a-target).abs() <= (b -target).abs() ? a : b);

```