unable to loop through key values of object and add them to a StringBuffer

im having issues encoding key value pairs inside of UrlFormatter.encode_params.
when i so it outside it works.
code1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
</code>
<code>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); </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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();
}
</code>
<code> 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(); } </code>
 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::

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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>
</code>
<code>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> </code>
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
});
</code>
<code>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); }); </code>
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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật