Home > database >  Flutter: URL/URI including '$' character
Flutter: URL/URI including '$' character

Time:06-15

there's an issue facing on URL that include '$' char.

is there another way to avoid it?

var url = Uri.parse( 'https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?$top=100&$skip=0&$orderby=Data desc&$format=json&$select=Indicador,DataReferencia,Mediana,baseCalculo');

CodePudding user response:

Dart has something called String Interpolation. Here's a snippet from the docs:

To put the value of an expression inside a string, use ${expression}. If the expression is an identifier, you can omit the {}.

Here are some examples of using string interpolation:

String Result
'${3 2}' '5'
'${"word".toUpperCase()}' 'WORD'
'$myObject' The value of myObject.toString()

In your case, the URL string contains exactly the escape symbol $ to make a String interpolation. Dart thinks all words after the $ symbol are variable names but it doesn't find them defined anywhere. To fix it just do what @jamesdlin suggested: Escape the $ symbols like \$ or prefix the string with r like below:

r'https://...Mensais?$top=100&$skip=0&$orderby=...'.

CodePudding user response:

Use this instead

var url = Uri.parse( 'https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?\$top=100&\$skip=0&\$orderby=Data desc&\$format=json&\$select=Indicador,DataReferencia,Mediana,baseCalculo');

Use a reverse slash to escape string

  • Related