我正在將專案添加到這樣的串列中。我有一個像這樣的名為 BuyHistoryModel 的檔案
class buyHistoryModel{
String symbol, ticketNumber, orderNumber,datePlaced, dateMatched, timeInForce,
bidPrice, matchPrice,
volume, status;
buyHistoryModel(
{
required this.symbol,
required this.ticketNumber,
required this.orderNumber,
required this.datePlaced,
required this.dateMatched,
required this.timeInForce,
required this.bidPrice,
required this.matchPrice,
required this.volume,
required this.status
}
);
}
然后我有一個資料構建器,我可以在其中提供資料
//data of buy history
import 'package:zse_direct_flutter/models/buyhistory_model.dart';
class BuyHistoryAdapter {
List getBuyHistory() {
return [
buyHistoryModel(
symbol: "FCA.ZW",
ticketNumber: "216188",
orderNumber: "419399",
datePlaced : "2021-10-01 10:19:23",
dateMatched: "02-SEP-21 10.19.12.607098 AM",
timeInForce: "0",
bidPrice: "3.45",
matchPrice: "3.45",
volume: "800",
status: "matched"
),
buyHistoryModel(
symbol: "ZSE.ZW",
ticketNumber: "",
orderNumber: "419399",
datePlaced : "2021-10-01 10:19:23",
dateMatched: "",
timeInForce: "0",
bidPrice: "3.45",
matchPrice: "",
volume: "800",
status: "cancel"
),
buyHistoryModel(
symbol: "SIM.ZW",
ticketNumber: "",
orderNumber: "419399",
datePlaced : "2021-10-01 10:19:23",
dateMatched: "",
timeInForce: "0",
bidPrice: "3.45",
matchPrice: "",
volume: "800",
status: "done"
),
];
}
}
然后在我的主檔案中,我將配接器中的專案添加到我的串列中
BuyHistoryAdapter pdb = new BuyHistoryAdapter();
List<buyHistoryModel> tmpList = <buyHistoryModel>[];
for(int i=0; i < pdb.getBuyHistory().length; i ) {
tmpList.add(pdb.getBuyHistory()[i]);
}
但是如果從配接器檔案中觀察,狀態是不同的。在這個頁面上,我想要一個代碼來檢查狀態是否匹配。如果是,則添加到串列中,否則不添加到串列中。像這樣的東西
for(int i=0; i < pdb.getBuyHistory().length; i ) {
if(status == "matched"){
tmpList.add(pdb.getBuyHistory()[i]);
}
}
這是完整的代碼
List<buyHistoryModel> buyHistoryList = [];
@override
void initState() {
BuyHistoryAdapter pdb = new BuyHistoryAdapter();
List<buyHistoryModel> tmpList = <buyHistoryModel>[];
for(int i=0; i < pdb.getBuyHistory().length; i ) {
tmpList.add(pdb.getBuyHistory()[i]);
}
setState(() {
buyHistoryList = tmpList;
_filteredList = buyHistoryList;
});
controller.addListener(() {
if(controller.text.isEmpty) {
setState(() {
filter = "";
_filteredList = buyHistoryList;
});
} else {
setState(() {
filter = controller.text;
});
}
});
super.initState();
}
我怎么做?
uj5u.com熱心網友回復:
為什么你所有的變數都是字串?價格不應該是雙倍的,日期日期時間和狀態不應該是列舉嗎?反正你問的不是這個。對您的問題的最簡單答案是使用該.where()方法。因此,擺脫 for 回圈并在其位置執行以下操作:
BuyHistoryAdapter pdb = new BuyHistoryAdapter();
var originalList = pdb.getBuyHistory();
var matchedList = originalList.where((model) => model.status == 'matched').toList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/334721.html
上一篇:如何修復顫振中的無效雙倍
