jamiewest / signalr_core

ASP.NET Core SignalR Dart Client
https://pub.dev/packages/signalr_core
MIT License
90 stars 58 forks source link

FormatException: Unexpected character error #65

Open aorhandev opened 3 years ago

aorhandev commented 3 years ago

Hello everyone,

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": "asd@asd.com", 
"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": "asd@asd.com", 
"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 ?

zigaspica commented 2 years ago

@aorhancom Same issue. How did you solve that? Looks like signalr_core library doesn't correctly parse json objects. If API sends "key1":"value1", signalr_core always deliver us key1:value1 without quotation marks which is not valid json at all! @jamiewest

zigapovhe commented 2 years ago

@aorhancom Hi Ahmed! I've just fixed this issue in my fork of this repository: https://github.com/zigapovhe/signalr_core In order to use this feature, you have to add a parameter to (hubConnection?.on(method, (arguments, rawArguments) {}