How do I parse query strings safely in Dart?
Let’s assume I have q string with the value of:
?page=main&action=front&sid=h985jg9034gj498g859gh495
Ideally the code should work both in the server and client, but for now I’ll settle for a working client-side code.
I have made a simple package for that purpose exactly: https://github.com/kaisellgren/QueryString
Example:
The result is a
Map. Accessing parameters is just a simpler['action']and accessing a non-existant query parameter isnull.Now, to install, add to your
pubspec.yamlas a dependency:And run
pub install.The library also handles decoding of things like
%20and+, and works even for empty parameters.It does not support "array style parameters", because they are not part of the RFC 3986 specification.