0

Hello i have a simple for loop what i want to combine the loop results into 1 string separated with a comma

   String loc;
   for(var q=0; q<2; q++){
       userData = res;
       loc = userData[q]['location']; 
       print(loc); //value in first loop is location A value in first second loop is location B i want to combine them on 1 string
      }
5
  • simply append values to loc. Commented Mar 5, 2020 at 6:46
  • loc= loc+userData[q]['location]; Commented Mar 5, 2020 at 6:46
  • You want to combine like appending string or create a new array of location? Commented Mar 5, 2020 at 6:52
  • [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '+' was called on null. Commented Mar 5, 2020 at 8:00
  • use String loc=""; Commented Mar 5, 2020 at 9:22

3 Answers 3

1

If you want just append in String then

void main() {
  String loc="";
  List<String> locs = ["a","d","B","C"];
   for(var q=0; q<2; q++){
       loc = (loc.length>0? (loc+", ") : loc)+ (locs[q] ?? " "); 
      }

print(loc); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '+' was called on null.
thank you but how could move the separator from the 1st to the last in my case i use comma Example ,Location A into Location A,
edited thank you i figured it out now , i just use this code loc = (userData[q]['location'] ?? " - ")+","+loc;
hello i have small problem in "Location C," how to remove the last comma it should be like this "Location A, Location B" not this "Location A, Location B,"
1

The traditional way would be to collect the strings into a string buffer, and manually add the comma:

var buffer = StringBuffer();
String separator = ""; // Avoid leading comma.
for (var data in userData) {
  buffer..write(separator)..write(data["location"]);
  separator = ",";
}
var loc = buffer.toString();

Another approach is to collect the values into a list and then use the join method:

var loc = [for (var data in userData) data["location"]].join(",");

That's probably more convenient in most cases, except when you need to do fancy computation on each element.

I have avoided using the for(;;) loop since you don't actually use the index for anything except accessing the user data. Then it's better to just for/in over the list elements directly.

Comments

0

Try ForEach

 void main() {
 List<String> places = ['New York', 'London', 'Berlin'];
 String linearPlace = '';
 places.forEach((thePlace) {
 linearPlace = linearPlace + thePlace + ", ";
 });
 print(linearPlace);
 //output:   New York, London, Berlin, 
 
 //   In case of Map
 Map<String, String> countryCapitalsMap = {'Austria': 'Vienna', 
 'Armenia': 'Yerevan', 'India': 'Delhi'};
   
 String countryWithCap = '';

   countryCapitalsMap.forEach((key, value) {
   countryWithCap = countryWithCap + key + ': ' + value + "\n";
   });
    print(countryWithCap);
 // output:
 // Austria: Vienna
 // Armenia: Yerevan
 // India: Delhi

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.