buildQueryString method

String buildQueryString(
  1. Map<String, String?>? data
)

Helper to build a Query String.

Implementation

String buildQueryString(Map<String, String?>? data) {
  if (data == null || data.isEmpty) return '';

  var query = StringBuffer();

  data.forEach((key, value) {
    var keyEncoded = Uri.encodeQueryComponent(key);
    var valueEncoded = value != null ? Uri.encodeQueryComponent(value) : '';
    var keyValue = '$keyEncoded=$valueEncoded';

    if (query.isNotEmpty) {
      query.write('&');
    }
    query.write(keyValue);
  });

  return query.toString();
}