简体   繁体   中英

Concatenate strings in flutter adds “”

This is rather a silly question but I can't seem to figure it out. I'm building a flutter app that communicates to an API and in order for the user to be authenticated it sends an token. Whenever the user logs in I save the token in the Shared Preferences and then when I make an request add the token to the headers or in this case I need it in the url. However, whenever I concatenate the strings: the server url and the token it adds extra "" to the token. something like:

http://10.0.2.2:64342/chatHub?access_token="token-value"

instead of

http://10.0.2.2:64342/chatHub?access_token=token-value

here's the code:

 var preferences = await SharedPreferences.getInstance();
 token = preferences.getString(token_key);
 var url = '$serverURl?access_token=$token';

As far as I understand your question, I would like to answer it.
That's not possible!

  var serverURl = 'http://10.0.2.2:64342/chatHub';
  var token = 'token-value';
  var url = '$serverURl?access_token=$token';
  print(url);

It just prints the correct one!
You can check the string that is stored in the SharedPreferences. That maybe with quotes.

Okay, I figured it out. Since I was sending only the token from the API. I was receiving it with the "" in it. Instead I now send a json with the token, like: { "token": "token_value"} and then decode it to get the actual value. So when I store it the shared preferences it doesn't keep the "".

So in the backend:

return Ok(new {token = generatedToken});

and in dart

var tokenJson = json.decode(response.body);
var token = tokenJson['token'];
preferences.setString(token_key, token);

Thanks to everyone that helped:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM