SignalR json api problem in Flutter (FormatException: Unexpected character (at character 4)

162

I did it. I used the below code and I received data. This code is come from my model.

List<Musteri> musteriler =
        List<Musteri>.from(data[0].map((model) => Musteri.fromJson(model)));
Share:
162
Admin
Author by

Admin

Updated on December 31, 2022

Comments

  • Admin
    Admin over 1 year

    I am new to flutter and I try use signalr at my project. Actually I succeed login, simple parameter and token. But I need multi parameter for my app. My friend prepared API for me. We must use one more parameter. I try same coding but I take a below error.

    I wonder where I make mistakes? I tried different models but everytime I take same error.

    My json model is

    [[
    
    {"aciklama": "Description 1", 
    "ad": "Ad 1", 
    "adres": "Adres 1", 
    "bolge": "Bölge 1", 
    "cepNo": "0123456789", 
    "ePosta": "[email protected]", 
    "faksNo": "01234567890", 
    "ilce": "İlçe 1", 
    "sehir": "Şehir 1", 
    "telefonNo": "01234567890", 
    "vergiDairesi": "Vergi Dairesi 1", 
    "vergiNo": "987654321", 
    "yetkili": "Yetkili 1"},
    
    {"aciklama": "Description 2", 
    "ad": "Ad 2", 
    "adres": "Adres 2", 
    "bolge": "Bölge 2", 
    "cepNo": "0123456789", 
    "ePosta": "[email protected]", 
    "faksNo": "01234567890", 
    "ilce": "İlçe 2", 
    "sehir": "Şehir 2", 
    "telefonNo": "01234567890", 
    "vergiDairesi": "Vergi Dairesi 2", 
    "vergiNo": "987654321", 
    "yetkili": "Yetkili 2"},
    
    
    ]]
    
    

    And then I use https://app.quicktype.io/. I convert to dart model. You can see below

    // To parse this JSON data, do
    //
    //     final musteri = musteriFromJson(jsonString);
    
    import 'dart:convert';
    
    List<Musteri> musteriFromJson(String str) =>
        List<Musteri>.from(json.decode(str).map((x) => Musteri.fromJson(x)));
    
    String musteriToJson(List<Musteri> data) =>
        json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
    
    class Musteri {
      Musteri({
        this.aciklama,
        this.ad,
        this.adres,
        this.bolge,
        this.cepNo,
        this.ePosta,
        this.faksNo,
        this.ilce,
        this.sehir,
        this.telefonNo,
        this.vergiDairesi,
        this.vergiNo,
        this.yetkili,
      });
    
      String aciklama;
      String ad;
      String adres;
      String bolge;
      String cepNo;
      String ePosta;
      String faksNo;
      String ilce;
      String sehir;
      String telefonNo;
      String vergiDairesi;
      String vergiNo;
      String yetkili;
    
      factory Musteri.fromJson(Map<String, dynamic> json) => Musteri(
            aciklama: json["aciklama"] == null ? null : json["aciklama"],
            ad: json["ad"] == null ? null : json["ad"],
            adres: json["adres"] == null ? null : json["adres"],
            bolge: json["bolge"] == null ? null : json["bolge"],
            cepNo: json["cepNo"] == null ? null : json["cepNo"],
            ePosta: json["ePosta"] == null ? null : json["ePosta"],
            faksNo: json["faksNo"] == null ? null : json["faksNo"],
            ilce: json["ilce"] == null ? null : json["ilce"],
            sehir: json["sehir"] == null ? null : json["sehir"],
            telefonNo: json["telefonNo"] == null ? null : json["telefonNo"],
            vergiDairesi:
                json["vergiDairesi"] == null ? null : json["vergiDairesi"],
            vergiNo: json["vergiNo"] == null ? null : json["vergiNo"],
            yetkili: json["yetkili"] == null ? null : json["yetkili"],
          );
    
      Map<String, dynamic> toJson() => {
            "aciklama": aciklama == null ? null : aciklama,
            "ad": ad == null ? null : ad,
            "adres": adres == null ? null : adres,
            "bolge": bolge == null ? null : bolge,
            "cepNo": cepNo == null ? null : cepNo,
            "ePosta": ePosta == null ? null : ePosta,
            "faksNo": faksNo == null ? null : faksNo,
            "ilce": ilce == null ? null : ilce,
            "sehir": sehir == null ? null : sehir,
            "telefonNo": telefonNo == null ? null : telefonNo,
            "vergiDairesi": vergiDairesi == null ? null : vergiDairesi,
            "vergiNo": vergiNo == null ? null : vergiNo,
            "yetkili": yetkili == null ? null : yetkili,
          };
    }
    
    
    
    

    And I wrote the codes as below in my view file.

    _signalRSantralService.connection.on("Musteriler", (data) {
          if (data != null) {
            var musteriler = musteriFromJson(data.toString());
            debugPrint("işlem tamamlandı");
            // musterilerStream.sink.add(musteriler);
          }
        });
    

    I clicked button and I received this message in terminal. This error : "FormatException: Unexpected character (at character 4)"

    
    I/flutter ( 9934): LogLevel.trace: (WebSockets transport) data received. String data of length '567'
    I/flutter ( 9934): LogLevel.error: A callback for the method musteriler threw error 'FormatException: Unexpected character (at character 4)
    I/flutter ( 9934): [[{aciklama: Açıklama, ad: Ad, adres: Adres, bolge: Bölge, cepNo: 012345678...
    I/flutter ( 9934):    ^
    I/flutter ( 9934): '.
    I/flutter ( 9934): LogLevel.trace: (WebSockets transport) data received. String data of length '11'
    
    
    

    How can I edit this error ?