Home > Mobile >  FormatException: Invalid character (at character 12) in Dart/flutter
FormatException: Invalid character (at character 12) in Dart/flutter

Time:06-21

I am trying to build the following URI in flutter but a got an error. the code:

void main() {
String APIHOST = 'hammbwdsc02/visoon-backend';
var uri = Uri.https(APIHOST, '/path', {'q': 'dart'});
print(uri); 
}

(this code is tested: https://dartpad.dev/?null_safety=true) output:

Uncaught Error: FormatException: Invalid character (at character 12)
hammbwdsc02/visoon-backend
           ^

Does someone know why am I getting this error?

CodePudding user response:

As explained by julemand101 in the comments, hostnames cannot contain the / character.

In your example the hostname should be hammbwsc02 and the visoon-backend should be added to the path. Like so:

void main() {
  String APIHOST = 'hammbwdsc02';
  var uri = Uri.https(APIHOST, '/visoon-backend/path', {'q': 'dart'});
  print(uri); 
}

The structure of the URL is explained in more detail here: https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL

  • Related