im having issues encoding key value pairs inside of UrlFormatter.encode_params.
when i so it outside it works.
code1:
List<Map<String, dynamic>> table_query_params = [];
Map<String, dynamic> tqp1 = {
'column': 'sys_id',
'operator': '=',
'value': "'${widget.column['sys_table_id1']}'"
};
table_query_params.add(tqp1);
Map<String, dynamic> tqp2 = {'limit': '10', 'query_type': "'limit_by'"};
table_query_params.add(tqp2);
//
StringBuffer encoded_params = StringBuffer();
print("aaaaaaa");
tqp2.forEach((key, value) {
print("key: $key");
print("value: $value");
String param = '$key!=true';
print("PARAM: $param");
encoded_params.write(param);
});
print("encoded_params: ${encoded_params}");
/////
String table_query_encoded_query =
UrlFormatter.encode_params(table_query_params);
code2:
static String encode_params(List<Map<String, dynamic>> key_value_pairs) {
StringBuffer encoded_params = StringBuffer();
for (Map<String, dynamic> pair in key_value_pairs) {
try {
if (!pair.containsKey('column')) {
print("NO COLUMN ${pair}");
pair.forEach((key, value) {
print("key: $key");
print("value: $value");
String param = '$key!=true';
print("PARAM: $param");
encoded_params.write(param);
});
} else {
String column = Uri.encodeComponent(pair['column'].toString());
String operator;
if (pair.containsKey('operator')) {
operator = Uri.encodeComponent(pair['operator'].toString());
} else {
operator = Uri.encodeComponent('=');
}
String value = '';
if (pair.containsKey('value')) {
if (pair['value'] is Map || pair['value'] is List) {
value = json.encode(pair['value']);
} else {
value = pair['value']?.toString() ?? '';
}
value = Uri.encodeComponent(value);
}
if (encoded_params.length > 0) {
encoded_params.write('&');
}
encoded_params.write('$column=$operator=$value');
}
} catch (e) {
print('Error encoding URL parameters: $e');
}
}
return encoded_params.toString();
}
output::
Restarted application in 82ms.
The debugEmulateFlutterTesterEnvironment getter is deprecated and will be removed in a future release. Please use `debugEmulateFlutterTesterEnvironment` from `dart:ui_web` instead.
_ui_type: record
_ui_view: admin_sys_menu_category
_query_params: sys_active=%3D=true
_get_view_columns()
_get_columns_data()
CKICK
_on_icon_click()
aaaaaaa
key: limit
value: 10
PARAM: limit!=true
key: query_type
value: 'limit_by'
PARAM: query_type!=true
encoded_params: limit!=truequery_type!=true
NO COLUMN {limit: 10, query_type: 'limit_by'}
key: limit
value: 10
PARAM: limit!=true
key: query_type
value: 'limit_by'
PARAM: query_type!=true
Error: RangeError (index): Index out of range: no indices are valid: 0
http://localhost:56797/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:49 throw_
http://localhost:56797/dart-sdk/lib/_internal/js_dev_runtime/private/js_array.dart 591:7 _get]
http://localhost:56797/dart-sdk/lib/internal/cast.dart 99:38 _get
http://localhost:56797/packages/worknow_front_end/ui/components/columns/list_field/list_field.dart 32:21 _on_icon_click
http://localhost:56797/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50 <fn>
http://localhost:56797/dart-sdk/lib/async/zone.dart 1661:54 runUnary
http://localhost:56797/dart-sdk/lib/async/future_impl.dart 156:18 handleValue
http://localhost:56797/dart-sdk/lib/async/future_impl.dart 840:44 handleValueCallback
http://localhost:56797/dart-sdk/lib/async/future_impl.dart 869:13 _propagateToListeners
http://localhost:56797/dart-sdk/lib/async/future_impl.dart 641:5 [_completeWithValue]
http://localhost:56797/dart-sdk/lib/async/future_impl.dart 715:7 callback
http://localhost:56797/dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop
http://localhost:56797/dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop
http://localhost:56797/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 181:15 <fn>
i know the issue is below but i dont know where.
if (!pair.containsKey('column')) {
print("NO COLUMN ${pair}");
pair.forEach((key, value) {
print("key: $key");
print("value: $value");
String param = '$key!=true';
print("PARAM: $param");
encoded_params.write(param);
});
i am printing the variables before adding it to encoded_params so not sure what the issue is.
i am able to iterate through the key/value pairs out side of Uri.encodeComponent.
i didnt find any similar stack overflows.
the expected result should be that the system does not crash.