सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
qs_dart एक Dart पुस्तकालय है जो प्रश्न डेटा के संकलन और अनुवाद के लिए उपयोग किया जाता है, जो लोकप्रिय JavaScript qs पुस्तकालय से प्रेरित है। यह URL प्रश्न पैरामीटर के विश्वसनीय विश्लेषण और अनुवाद की सुविधा प्रदान करता है, जिसमें नेस्टेड ऑब्जेक्ट, ऐरे और कस्टम अलगाव का समर्थन होता है। वेब और मोबाइल एप्लिकेशन में HTTP रिक्वेस्ट और रिप्लाई डेटा को संभालने के लिए आदर्श है, जो प्लेटफॉर्म के बीच सुसंगत प्रश्न डेटा फॉर्मेटिंग सुनिश्चित करता है। पैकेज हल्का, अच्छी तरह से परीक्षण किया गया है और सभी प्रमुख Flutter प्लेटफॉर्म के साथ संगत है।
उपयोग
URL क्वेरी पैरामीटर्स को पार्स करना
गतिशील API URL बनाना
HTTP रिक्वेस्ट में फॉर्म डेटा का प्रबंधन
नेस्टेड ऑब्जेक्ट्स को क्वेरी स्ट्रिंग में सीरियलाइज़ करना
REST APIs के साथ अंतरकार्यक्षमता
मुख्य विशेषताएँ
नेस्टेड ऑब्जेक्ट संकोडिंग का समर्थन करता है
क्वेरी में एरे सिंटैक्स का समर्थन करता है
कस्टमाइज़ेबल डिलीमीटर और गहराई के विकल्प
टिकाऊ त्रुटि संभाल
प्लेटफॉर्म के अनुकूलता
इनके लिए उपयुक्त
वेब और मोबाइल ऐप विकासकर्ता
API एकीकरण
फॉर्म डेटा प्रोसेसिंग
बैकएंड संगत क्वेरी स्ट्रिंग हैंडलिंग
विचार योग्य बातें
जटिल नेस्टेड संरचनाओं के लिए सावधानीपूर्वक कॉन्फ़िगर करने की आवश्यकता हो सकती है
पूर्ण URL संशोधन के लिए नहीं डिज़ाइन किया गया
केवल क्वेरी स्ट्रिंग ऑपरेशन्स तक सीमित
किनारे के मामलों के लिए बाहरी JS प per व्यवहार पर निर्भर है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
qs_dart
A query string encoding and decoding library for Dart.
Multiple list formats: indices (a[0]=x), brackets (a[]=x), repeat (a=x&a=y), comma (a=x,y) with optional comma round‑trip.
Dot notation: parse/encode a.b=c as nested; options to encode or decode literal dots in keys (encodeDotInKeys / decodeDotInKeys).
Charset & sentinel: UTF‑8 (default) and Latin‑1; charsetSentinel (utf8=✓) to auto‑detect encoding.
Hooks & controls: custom encoder/decoder, encode keys‑only or values‑only, alternate delimiters (string or RegExp), ignore leading ?.
Duplicates & empties: strategies for duplicates (combine/first/last); allow empty list items (foo[]) with allowEmptyLists.
Null handling: strictNullHandling, skipNulls, and clear empty‑string behavior.
Safety limits: configurable depth, strictDepth, parameterLimit, listLimit, and parseLists toggle.
Dates & ordering: pluggable serializeDate, stable key ordering with sort, selective output via filter.
RFC modes: spaces as %20 (RFC 3986, default) or + (RFC 1738).
Dart‑friendly: Uri.queryParametersQs() and Uri.toStringQs() extensions for idiomatic usage in Dart/Flutter.
Installation
Add the package to your project:
For Dart projects:
dart pub add qs_dart
For Flutter projects:
flutter pub add qs_dart
Agent skill
This repo includes an optional agent skill at
skills/qs-dart/ for users who want Codex or another
compatible agent to help prepare qs_dart encoding and decoding snippets. The skill is user-facing: it focuses on Dart
and Flutter usage, option selection, query-string interoperability, and edge-case caveats.
[decode] allows you to create nested [Map]s within your query strings, by surrounding the name of sub-keys with
square brackets []. For example, the string 'foo[bar]=baz' converts to:
By default, nested [Map]s are decoded up to 5 levels deep. For example, decoding
a string like 'a[b][c][d][e][f][g][h][i]=j' yields the following [Map]:
The depth limit helps mitigate abuse when [decode] is used to parse user input, and it is recommended to keep it a
reasonably small number. [DecodeOptions.strictDepth] adds a layer of protection by throwing a [RangeError] when the
limit is exceeded, allowing you to catch and handle such cases.
For similar reasons, by default [decode] parses up to 1000 parameters. Override with
[DecodeOptions.parameterLimit]:
Decode percent-encoded dots in keys with [DecodeOptions.decodeDotInKeys].
Note: it implies [DecodeOptions.allowDots], so [decode] will error if you set [DecodeOptions.decodeDotInKeys]
to true, and DecodeOptions.allowDots to false.
Some services add an initial utf8=✓ value to forms so that old Internet Explorer versions are more likely to submit the
form as utf-8. Additionally, the server can check the value against wrong encodings of the checkmark character and detect
that a query string or application/x-www-form-urlencoded body was not sent as utf-8, eg. if the form had an
accept-charset parameter or the containing page had a different character set.
QS supports this mechanism via the [DecodeOptions.charsetSentinel] option.
If specified, the utf8 parameter will be omitted from the returned [Map].
It will be used to switch to [latin1] or [utf8] mode depending on how the checkmark is encoded.
Important: When you specify both the [DecodeOptions.charset] option and the [DecodeOptions.charsetSentinel] option,
the [DecodeOptions.charset] will be overridden when the request contains a utf8 parameter from which the actual charset
can be deduced. In that sense the [DecodeOptions.charset] will behave as the default charset rather than the authoritative
charset.
Note that the only difference between an index in a [List] and a key in a [Map] is that the value between the brackets
must be a number to create a [List]. When creating [List]s with specific indices, [decode] will compact a sparse
[List] to only the existing values preserving their order:
[decode] will also limit specifying indices in a [List] to a maximum index of 20.
Any [List] members with an index of greater than 20 will instead be converted to a [Map] with the index as the key.
This is needed to handle cases when someone sent, for example, a[999999999] and it will take significant time to iterate
over this huge [List].
[DecodeOptions.listLimit] is a maximum element count. With the default limit of
20, a[19]=b can still become a [List], while a[20]=b falls back to a [Map].
To disable List parsing entirely, set [DecodeOptions.parseLists] to false.
Note: when a value is a ByteBuffer, it is still decoded using the selected
charset even when encode is false, so the emitted value reflects the buffer
contents.
Encoding can be disabled for keys by setting the [EncodeOptions.encodeValuesOnly] option to true:
Examples beyond this point will be shown as though the output is not URI encoded for clarity.
Please note that the return values in these cases will be URI encoded during real usage.
When [List]s are encoded, they follow the [EncodeOptions.listFormat] option, which defaults to [ListFormat.indices]:
Note: When using [EncodeOptions.listFormat] set to [ListFormat.comma], you can also pass the [EncodeOptions.commaRoundTrip]
option set to true or false, to append [] on single-item [List]s, so that they can round trip through a parse.
Set [EncodeOptions.commaCompactNulls] to true with the same format when you'd like to drop null entries instead of keeping empty slots (for example, [true, false, null, true] becomes true,false,true).
When [Map]s are encoded, by default they use bracket notation:
Caveat: when both [EncodeOptions.encodeValuesOnly] and [EncodeOptions.encodeDotInKeys] are true, only dots in keys are encoded; values remain unchanged.
You may allow empty [List] values by setting the [EncodeOptions.allowEmptyLists] option to true:
If you only want to override the serialization of [DateTime] objects, you can provide a custom [DateSerializer] in the
[EncodeOptions.serializeDate] option:
Finally, you can use the [EncodeOptions.filter] option to restrict which keys will be included in the encoded output.
If you pass a [Function], it will be called for each key to obtain the replacement value.
Otherwise, if you pass a [List], it will be used to select properties and [List] indices to be encoded:
To distinguish between null values and empty [String]s use the [EncodeOptions.strictNullHandling] flag.
In the result string the null values have no = sign:
You can use the [EncodeOptions.charsetSentinel] option to announce the character by including an utf8=✓ parameter with
the proper encoding of the checkmark, similar to what Ruby on Rails and others do when submitting forms.
By default, the encoding and decoding of characters is done in [utf8], and [latin1] support is also built in via
the [EncodeOptions.charset] and [DecodeOptions.charset] parameter, respectively.
If you wish to encode query strings to a different character set (i.e.
Shift JIS) you can use the euc package
expect(
QS.encode(
{
'a': 'こんにちは!',
},
EncodeOptions(
encoder: (str, {Encoding? charset, Format? format}) {
if ((str asString?)?.isNotEmpty ?? false) {
finalUint8List buf = Uint8List.fromList(
ShiftJIS().encode(str!),
);
finalList<String> result = [
for (int i = 0; i < buf.length; ++i) buf[i].toRadixString(16)
];
return'%${result.join('%')}';
}
return'';
},
),
),
equals('%61=%82%b1%82%f1%82%c9%82%bf%82%cd%81%49'),
);
The default [EncodeOptions.format] is [Format.rfc3986] which encodes ' ' to %20 which is backward compatible.
You can also set the [EncodeOptions.format] to [Format.rfc1738] which encodes ' ' to +.