Update one List from values another List

Viewed 30

I wrote the code what get Json data and put in listPc. And wrote the loop that add value 'hostName' form listPc to pcBusy List. But my code only add values to second list, If I press GetButton values in pcBusy list duplicates. I need to update the pcBusy List, not only add the same values.

This print if I press button two times:

[S14, S18, S19, S12, S02, V08, S01, O09, S14, S18, S19, S12, S02, V08, S01, O09]

Thanks for help!)

void fetchDataStandart() async {
  final urlAuth =
      Uri.parse('http://XXX.XX.XXX.XXX/api/usersessions/activeinfo');
  final response = await http
      .get(urlAuth, headers: <String, String>{'authorization': basicAuth});

  if (response.statusCode == 200) {
    List listPc = List.from(json.decode(response.body)['result']);

    for (int i = 0; i < listPc.length; i++) {
      pcBusy.add(listPc[i]['hostName']);
    }
    print(pcBusy);
  } else {
    throw Exception('Ошибка получения данных');
  }
}
2 Answers

TLDR: add Future<void> as your function return type and consider invoking pcBusy.clear() before overwriting with new data (depends on your logic, though).

With a little more context you'd help me giving you a more complete answer, but here's what I can see from your code:

  1. Your button adds data as many times as you're pressing it. IF you press it two times, you'll get "double" the data, sooner or later. This happens because you use the add method, which just appends data on your list. You can either reset the values with pcBusy.clear() before you add values or do something else if you think that this function shouldn't be overwriting your list. This really depends on your logic;
  2. You're awaiting a Future (via the async keyword), yet your Function doesn't return a Future. This means that - most likely - somewhere else you're awaiting for this function that in reality doesn't need to be awaited. As a consequence this means that when you first press the button, i.e. you fire the future, you can't await for it to happen and your UI doesn't update. The second time, it does update your UI with the previous result and the Future is fired again, letting it update your list with twice the values again as explained in step (1).

Hope this helps. EDIT. Here's some edited code:

// we want this function to be awaited: let it be a Future<void> async function
Future<void> fetchDataStandart() async {
  // ... firing an async HTTP request

  if (response.statusCode == 200) {
    // if everything is OK, decode the JSON
    List listPc = List.from(json.decode(response.body)['result']);

    // the following will OVERWRITE the list.
    pcBusy.clear(); // Or maybe save previous data somewhere else?
    for (int i = 0; i < listPc.length; i++) {
      pcBusy.add(listPc[i]['hostName']);
    }
    print(pcBusy);
  } else {
    throw Exception('Ошибка получения данных');
  }
}

To remove duplicates you can convert the list to a set then back to a list again.

pcBusy.toSet().toList();
Related