Nitneuq
Nitneuq

Reputation: 5012

How can I iterate this function for each index of List in flutter?

I have a complex solution to extract date and hour from a list and add the date and hour in regex to found associated value and save all in a sharedpreference. It's work If I focus manualy a specific index of my input List.

But my problem is I don't knwo how to iterate my code for each index of my input List[]

I have in input a List of date and hour

[2020-09-05 10:00,2020-09-06 08:02,2020-09-07 03:10]

I select first index [1] => 2020-09-05 10:00

I extract with regex the date and hour

match_date = 2020-09-05
match_hour = 08

and with a regex, I search in an other file, the value associated with the date and hour

Now I have my date hour and value I can save it in sharedpreference key(date,hour) value(value found)

But this approach works only with determinated index, here index [1] but if I have n index, I want to do this function n time

I try this

for (var i = 0; i < MyList.length; i++) {

 List<String> MyList ;  

    MyList= prefs.getStringList("save_list"); // load list  [2020-09-05 10:00,2020-09-06 08:02,2020-09-07 03:10]
      final MylistasMap = MyList.asMap(); //convert list in map 

      RegExp regExp1 = new RegExp(
        r'(\d{4}-\d{2}-\d{2})',);    
      var match_date = regExp1.firstMatch(MylistasMap[i]);  // match only date format XXXX-XX-XX of index[1] of MyList [2020-09-05,2020-09-06,2020-09-07]
      if(match_date!=null ) {
        var date = match_date.group(1);

        RegExp regExp2 = new RegExp(
          r'(\d*):',                        
        );
        var match_hour = regExp2.firstMatch(MylistasMap[i]);  // match only hour of index[1]

        if (match_hour != null) {

          var hour = match_hour.group(1);

          RegExp regExp3 = new RegExp(
            r'((?<='+date+',conso_h'+hour+':)[1-9]\S[^"]*)',  // match double value in other_file, saved with specific date, hour found previously in the list
          );
          var match_value = regExp3.firstMatch("$other_file");  // other_file is a String with all date and value saved

          if (match_value != null) {

            var value_conso = match_value.group(1);
            double val_double_value_conso = double.parse(value_conso);
            prefs.setDouble('$date,conso_h$hour', val_double_value_conso);
          }

        }
      }

}

Upvotes: 1

Views: 157

Answers (1)

Adithya Shetty
Adithya Shetty

Reputation: 1287

I wrote a program that will iterate through all the list items and print the date and hrs:

I removed the otherFile statement because I did not know what was in the other file, but you can continue and add the other file and pass other files.

myList.forEach((element) {

    RegExp regExp1 = new RegExp(
      r'(\d{4}-\d{2}-\d{2})',
    );
    var match_date = regExp1.firstMatch(
        element); // match only date format XXXX-XX-XX of index[1] of MyList [2020-09-05,2020-09-06,2020-09-07]
    if (match_date != null) {
      var date = match_date.group(1);

      RegExp regExp2 = new RegExp(
        r'(\d*):',
      );
      print('Date: $date');
      var match_hour =
          regExp2.firstMatch(element); // match only hour of index[1]

      if (match_hour != null) {
        var hour = match_hour.group(1);
print('Hours: $hour');
        RegExp regExp3 = new RegExp(
          r'((?<=' +
              date +
              ',conso_h' +
              hour +
              ':)[1-9]\S[^"]*)', // match double value in other_file, saved with specific date, hour found previously in the list
        );
      }
    }
  });

Wrap the above code in a function passing the list and the other files where the list is written.


Whole Code : I also have removed MyList= prefs.getStringList("save_list"); and used a sample list instead.

void main() {
  List<String> myList = [
    '2020-09-05 10:00',
    '2020-09-06 08:02',
    '2020-09-07 03:10'
  ];


  myList.forEach((element) {

    RegExp regExp1 = new RegExp(
      r'(\d{4}-\d{2}-\d{2})',
    );
    var match_date = regExp1.firstMatch(
        element); // match only date format XXXX-XX-XX of index[1] of MyList [2020-09-05,2020-09-06,2020-09-07]
    if (match_date != null) {
      var date = match_date.group(1);

      RegExp regExp2 = new RegExp(
        r'(\d*):',
      );
      print('Date: $date');
      var match_hour =
          regExp2.firstMatch(element); // match only hour of index[1]

      if (match_hour != null) {
        var hour = match_hour.group(1);
print('Hours: $hour');
        RegExp regExp3 = new RegExp(
          r'((?<=' +
              date +
              ',conso_h' +
              hour +
              ':)[1-9]\S[^"]*)', 
        );
      }
    }
  });
}

Upvotes: 1

Related Questions