Home > Mobile >  Flutter, how to determine if an adapter contains a string before adding to List
Flutter, how to determine if an adapter contains a string before adding to List

Time:10-24

I am adding items to a list like this. I have a file called BuyHistoryModel like this

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

  }
  );

}

Then i have a data builder where i feed data which is this

//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"

      ),

    ];
    }
    }

Then in my main file i am adding the items in the adapter to my list like this

BuyHistoryAdapter pdb = new BuyHistoryAdapter();
    List<buyHistoryModel> tmpList = <buyHistoryModel>[];


    for(int i=0; i < pdb.getBuyHistory().length; i  ) {

      tmpList.add(pdb.getBuyHistory()[i]);

    }

But if you observe from by adapter file, the statuses are different. And on this page i would like a code that checks whether the status is matched or not. If it is, it adds to the list, if not it does not add to the list. Something like this

for(int i=0; i < pdb.getBuyHistory().length; i  ) {
        
          if(status == "matched"){
             tmpList.add(pdb.getBuyHistory()[i]);
          }
    
        }

This is the full code

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();
  }

How do i do that?

CodePudding user response:

Why are all your variables Strings? Shouldn't the prices be doubles, the dates DateTimes, and the status an enum? Anyway, that is not what you asked. The easiest answer to your question is by using the .where() method. So get rid of the for loop and in its place do the following:

BuyHistoryAdapter pdb = new BuyHistoryAdapter();
var originalList = pdb.getBuyHistory();
var matchedList = originalList.where((model) => model.status == 'matched').toList();
  • Related