Flutter create API Request and receive Strings with - 'Ä' 'Ö' 'Ü' 'ß'

2,598

I used the build_value serializer from Dart. Therefore, an object is created from the jSON string if json.decode () and serialization was successful.

The problem of the special characters was solved with the last line in this code.

Thanks again!

//Objekte festlegen
RenderEbeneErste uiUeberprueft;

//jSON auslesen, prüfen & Objekt erstellen
String jsonURL = 'http://xxx.xxx.xxx.xxx/eingeleseneJSON.json';
final ausgeleseneDaten = await http.get(Uri.parse(jsonURL));
uiUeberprueft = serializers.deserializeWith(
  RenderEbeneErste.serializer, json.decode(utf8.decode(ausgeleseneDaten.bodyBytes)));
Share:
2,598
Markus Bach
Author by

Markus Bach

Updated on December 09, 2022

Comments

  • Markus Bach
    Markus Bach over 1 year

    Is there an easy way to query german letters like 'ä', 'ö' 'ü' in an API with Darts http - API (in Flutter)?

    An API query should download a jSON string to a Flutterapp. Unfortunately, the jSON string contains German special characters.

    String einleseURL = 'http://xxx.xxx.xxx.xxx/einlesen.json';
    final ausgeleseneJsonString = await http.get(Uri.encodeFull(einleseURL));
    uiUeberprueft = serializers.deserializeWith(
      RenderEbeneErste.serializer, json.decode(ausgeleseneJsonString.body));
    

    The only solution I can imagine is to convert the jSON String in utf-8's numeric values and save this new file at the server for http query. The list of numbers is then called by Flutter and decrypted with utf.decoode () before a json.decode () occurs.

    please refer: How can I convert string to utf8 in Dart?

    Is there a simpler way?

    • Günter Zöchbauer
      Günter Zöchbauer about 5 years
      There is nothing special about german special characters in regard to an HTTP request. What is the actual result you get?
    • Markus Bach
      Markus Bach about 5 years
      Dear Günter, thank you for response. The actual result is for examle: ' "nutzerHilfe": "Beschreibung hinzufügen", '. Also the generated UI in Flutter look exactly like the output debugPrint (ausgeleseneJsonString.body) on the console.
    • Günter Zöchbauer
      Günter Zöchbauer about 5 years
      What do you get if you load the URL in the browser? Perhaps the charset part in the content-type header sent by the server is not correct. Perhaps the string is not even UTF-8 encoded but some other encoding?
    • Markus Bach
      Markus Bach about 5 years
      The query runs via IIS on a computer. The .json file is encoded as utf-8 in Notepad ++. In the browser, the string is displayed correctly, even on a different device.
    • Günter Zöchbauer
      Günter Zöchbauer about 5 years
      What is the charset header?
    • Markus Bach
      Markus Bach about 5 years
      Honestly, I do not know where to read this. Btw. If I enter the jSON file of the app directly as 'asset' and thus bypass the API request, it works fine.
    • Günter Zöchbauer
      Günter Zöchbauer about 5 years
      If you open the network tab in Chrome devtools before you make the request you should be able to see the headers.
    • Günter Zöchbauer
      Günter Zöchbauer about 5 years
    • Markus Bach
      Markus Bach about 5 years
      Many Thanks. With slightly modified source code from the linked page it runs. Thank you! If anyone is interested in the source code, I like to post.
    • Günter Zöchbauer
      Günter Zöchbauer about 5 years
      Yes please answer your question with the code that worked for you
  • Antonio Reyes
    Antonio Reyes about 4 years
    Using the http.dart package the json.decode(utf8.decode(response.bodyBytes) works!!