Flutter 中的 SignalR json api 问题(FormatException:意外字符(在字符 4 处)

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

我是 flutter 的新手,我尝试在我的项目中使用 signalr。其实我成功登录,简单的参数和令牌。但是我的应用程序需要多个参数。我的朋友为我准备了API。我们必须再使用一个参数。我尝试相同的编码,但出现以下错误。

请问我哪里出错了?我尝试了不同的模型,但每次都出现相同的错误。

我的json模特是

[[

{"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"},


]]

然后我使用https://app.quicktype.io/。我转换为飞镖模型。你可以在下面看到

// 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,
      };
}



我在视图文件中写了如下代码。

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

我点击了按钮,我在终端中收到了这条消息。此错误:“FormatException:意外字符(在字符 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'


如何修改此错误?

您 JSON 数据包含嵌套列表。你需要做:

json.decode(data[0]) 而不是 json.decode(data).

我做到了。我使用了以下代码并收到了数据。此代码来自我的模型。

List<Musteri> musteriler =
        List<Musteri>.from(data[0].map((model) => Musteri.fromJson(model)));