updateQuery method

void updateQuery(
  1. Map<String, dynamic> queryParametersToUpdate, {
  2. bool mergeValues = false,
})
inherited

Update the URI query parameters, merging the given map with the current query parameters map instead of overwriting it.

Throws an ArgumentError if any value in the given queryParametersToUpdate map is not a String or an Iterable<String>.

If mergeValues is false, each parameter in queryParametersToUpdate will be completely replaced with the new value.

If mergeValues is true, the values for each parameter in queryParametersToUpdate will be merged into the existing list of values for that parameter. Duplicate values will be discarded.

Implementation

void updateQuery(
    Map<String, dynamic /*String|Iterable<String>*/ > queryParametersToUpdate,
    {bool mergeValues = false}) {
  final newQueryParameters = <String, Set<String>>{};

  // Copy the current query param values.
  queryParametersAll.forEach((key, value) {
    newQueryParameters[key] = value.toSet();
  });

  // Update the query using the given params.
  queryParametersToUpdate.forEach((key, value) {
    // Initialize or reset the value list if it either does not already exist,
    // or if we're not merging values.
    if (!mergeValues || !newQueryParameters.containsKey(key)) {
      newQueryParameters[key] = <String>{};
    }

    // Add the param value(s) while eliminating duplicates.
    // Throw an ArgumentError if any value is invalid.
    if (value is String) {
      newQueryParameters[key]!.add(value);
    } else if (value is Iterable<String>) {
      newQueryParameters[key]!.addAll(value);
    } else {
      throw ArgumentError('Query parameter "$key" has value "$value" '
          'which is not a String or an Iterable<String>.');
    }
  });

  _uri = _uri.replace(queryParameters: newQueryParameters);
}