首页 > 解决方案 > 为什么在 Flutter 中为 xml 解析返回 null 模型类列表?

问题描述

我正在使用 Flutter xml 包在 Flutter 上解析这个 xml 文件。我创建了这样的模型类,但是当我访问 cList[index].. 时返回 null。如果我snapshot.data.tarih在 main.dart 访问,它的工作。但是 Currency 类变量返回 null 给我。我在哪里做错了?谢谢你。

XML:

 <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="isokur.xsl"?>
<Tarih_Date Tarih="30.11.2020" Date="11/30/2020"  Bulten_No="2020/227" >
    <Currency CrossOrder="0" Kod="USD" CurrencyCode="USD">
            <Unit>1</Unit>
            <Isim>ABD DOLARI</Isim>
            <CurrencyName>US DOLLAR</CurrencyName>
            <ForexBuying>7.7892</ForexBuying>
            <ForexSelling>7.8032</ForexSelling>
            <BanknoteBuying>7.7837</BanknoteBuying>
            <BanknoteSelling>7.8149</BanknoteSelling>
            <CrossRateUSD/>
            <CrossRateOther/>
        
    </Currency>
    <Currency CrossOrder="1" Kod="AUD" CurrencyCode="AUD">
            <Unit>1</Unit>
            <Isim>AVUSTRALYA DOLARI</Isim>
            <CurrencyName>AUSTRALIAN DOLLAR</CurrencyName>
            <ForexBuying>5.7366</ForexBuying>
            <ForexSelling>5.7740</ForexSelling>
            <BanknoteBuying>5.7102</BanknoteBuying>
            <BanknoteSelling>5.8087</BanknoteSelling>
                <CrossRateUSD>1.3546</CrossRateUSD>
                <CrossRateOther/>
        
    </Currency>
    <Currency CrossOrder="2" Kod="DKK" CurrencyCode="DKK">
            <Unit>1</Unit>
            <Isim>DANİMARKA KRONU</Isim>
            <CurrencyName>DANISH KRONE</CurrencyName>

rate.model.dart

import 'package:xml/xml.dart';

class TarihDate {
  String tarih;
  List<Currency> cList;

  TarihDate._(this.tarih, this.cList);

  factory TarihDate.fromElement(XmlElement xmlElement) {
    return TarihDate._(
        xmlElement.getAttribute("Tarih"),
        xmlElement
            .findElements("Currency")
            .map((e) => Currency.fromElement(e))
            .toList());
  }
}

class Currency {
  String currencyName;
  String forexBuying;
  String forexSelling;
  String banknotBuying;
  String banknotSelling;

  Currency._(this.currencyName, this.forexBuying, this.forexSelling,
      this.banknotBuying, this.banknotSelling);

  factory Currency.fromElement(XmlElement genreElement) {
    return Currency._(
        genreElement.getAttribute('CurrencyName'),
        genreElement.getAttribute('ForexBuying'),
        genreElement.getAttribute('ForexSelling'),
        genreElement.getAttribute('BanknotBuying'),
        genreElement.getAttribute('BanknotSelling'));
  }
}

rate.service.dart

import 'package:currency_rates/model/rates_model.dart';
import 'package:http/http.dart' as Http;
import 'package:xml/xml.dart' as xml;

String url = "https://www.tcmb.gov.tr/kurlar/today.xml";

Future<TarihDate> getJsonFromUrl() async {
  var response = await Http.get(url);
  var raw = xml.XmlDocument.parse(response.body);
  var elements = raw.getElement("Tarih_Date");
  return TarihDate.fromElement(elements);
}

主要.dart

body: FutureBuilder(
          future: getJsonFromUrl(),
          builder: (context, AsyncSnapshot<TarihDate> snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting)
              return Center(child: CircularProgressIndicator());
            if (snapshot.hasData) {
              print(snapshot.data.cList[0].currencyName);
              return ListView.builder(
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(
                        snapshot.data.cList[index].forexBuying,
                        style: TextStyle(color: Colors.black),
                      ),
                    );
                  },
                  itemCount: snapshot.data.cList.length);
            }
            return ErrorWidget(throw Exception("Error"));
          }),

标签: xmlflutterdartxml-parsingmobile-development

解决方案


我认为Currentcy.fromElement你应该从子节点读取,而不是属性。例如,替换genreElement.getAttribute('CurrencyName')genreElement.getElement('CurrencyName').text

BanknoteBuying注意:在同一方法(和BanknoteSelling)中也有一些 XML 名称的拼写错误。


推荐阅读