Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't assign array to string variable in flutter

 for (i = 0; i < arr1.length - 1; i++) {
  msg += arr1[i] + ",";
}
msg += arr1[i];

I want to throw the data from the above array into a string variable named msg with a comma between them. But I am getting an error as below. How can I fix.

_TypeError (type ‘String’ is not a subtype of type ‘num’ of ‘other’)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It depends of what type of data is store in the array. If integers, or whatever else, you need to tranform to a string, with the toString() method

So:

msg += arr1[i].tostring() + ",";

If mixed types, you can check type with something like this:

if (arr1[i] is int) {
  msg += arr1[i].tostring() + ",";
}

if (arr1[i] is bool) {
  if (arr1[i]) { 
    msg += "1,";
  } else {
    msg += "0,";
}
 // And so on...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading