【问题标题】:SignalR json api problem in Flutter (FormatException: Unexpected character (at character 4)Flutter 中的 SignalR json api 问题 (FormatException: Unexpected character (at character 4)
【发布时间】:2021-10-02 11:48:28
【问题描述】:

我是 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: 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'


如何编辑此错误?

【问题讨论】:

    标签: json flutter api dart signalr


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我做到了。我使用了下面的代码并收到了数据。这段代码来自我的模型。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-26
        • 2020-09-12
        • 2020-10-20
        • 1970-01-01
        • 2019-10-08
        • 2021-06-17
        • 2011-08-16
        相关资源
        最近更新 更多