Home > database >  Dart - how to send query parameter array with brackets
Dart - how to send query parameter array with brackets

Time:10-20

Using Flutter/Dart- i'd like to make a Uri network call with queryParameters that include arrays.

Ex:

Map<String, dynamic> queryParameters = {
        'include': 'messages',
        'scopes': ['withHasUnreadMessages', 'withSubscriberUserIds'],
};
uri = Uri.https(_getBaseUrl(), '/mypath', queryParameters);

Which prints:

https://mypath?include=subscribers&scopes=withHasUnreadMessages&scopes=withSubscriberUserIds

However, my api server (PHP) doesn't play nicely with requests that include duplicate keys. My server wants:

https://mypath?include=subscribers&scopes[]=withHasUnreadMessages&scopes[]=withSubscriberUserIds

Is there a way to add brackets to darts Uri queryParameters?

PS - This might have been a comment https://stackoverflow.com/a/57367680/6010500 but i don't have the reputation points.

CodePudding user response:

Not a dart user, but from first glance, you can add the array brackets to the input name

Map<String, dynamic> queryParameters = {
        'include': 'messages',
        'scopes[]': ['withHasUnreadMessages', 'withSubscriberUserIds'],
};
uri = Uri.https(_getBaseUrl(), '/mypath', queryParameters);
  • Related