when opening a HttpRequest I get this error: 2 positional arguments expected, but 5 found

StackOverflow https://stackoverflow.com/questions/17505748

  •  02-06-2022
  •  | 
  •  

Pregunta

I have written a function to submit a form to a REST API. Here is the code:

HttpRequest request;
void submitForm(Event e) {
   e.preventDefault(); // Don't do the default submit.

   request = new HttpRequest();

   request.onReadyStateChange.listen(onData); 

   // POST the data to the server.
   var url = 'http://127.0.0.1:8000/api/v1/users';
   request.open('GET', url, true, theData['userName'], theData['password']);
   request.send();
}

From the documentation you can have five arguments as follows when you open the request:

void open(String method, String url, {bool async, String user, String password})

See here for details.

As you can see I have utilized all 5 arguments allowed but for some reason I get this error:

2 positional arguments expected, but 5 found

Any suggestions as to why?

¿Fue útil?

Solución

Normal arguments are called positional arguments (like method and url in this case). The arguments in braces are optional named parameters:

void open(String method, String url, {bool async, String user, String password})

They are optional, you don't need to pass them if you don't need them. The order isn't important while calling. If you need to pass them, prefix them with the name and a colon. In your case:

request.open('GET', url, async: true, user: theData['userName'], password: theData['password']);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top