xml - 为什么在 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"));
}),
解决方案
我认为Currentcy.fromElement
你应该从子节点读取,而不是属性。例如,替换genreElement.getAttribute('CurrencyName')
为genreElement.getElement('CurrencyName').text
。
BanknoteBuying
注意:在同一方法(和BanknoteSelling
)中也有一些 XML 名称的拼写错误。
推荐阅读
- python-3.x - 在python中使用多线程请求图像时出现操作系统错误
- python - 牛顿法收敛但我得到无界错误
- css - 绝对定位元素内的图像
- c# - 检查俱乐部系统中用户的多个角色
- git - 即使在 Azure DevOps 中连接到项目后,也无法看到更改分支拉取请求同步
- c++ - 一次读取一个文件时出现一个错误
- json - 安全的 JSON 解码。仍然找不到密钥:没有与密钥关联的值'
- amazon-web-services - AWS Elastic Beanstalk 100.0% 的请求以 HTTP 5xx 失败
- java - 在二维数组的末尾添加新列
- swift - Swift 月周数