How do I convert List into a String
How do I then use _checked string value in other place
List<String> _checked = [];
How do I convert List into a String
How do I then use _checked string value in other place
List<String> _checked = [];
There are a bunch of ways. The first way is to simply call toString
on the list:
final list = ['a', 'b', 'c'];
final str = list.toString();
// str = "[a, b, c]"
Another way is to use join
, which will concatenate all the strings into a single string using an optional separator:
final list = ['a', 'b', 'c'];
final str = list.join('-');
// str = "a-b-c"
Other more specialized methods exist, but they require knowing more about what you want the output to look like.
The join
method is used for this. The array elements are converted to strings and then glued into one, in the arguments join
you can pass a section that will be between the glued parts.
List<String> list = ["a", "b", "c"];
list.join() // 'abc';
list.join('|') // 'a|b|c'; // better for comparisons
Other way - use class:
import 'package:crypto/crypto.dart';
import 'package:convert/convert.dart';
import 'dart:convert';
class A {
List<String> a;
A(List<String> list): a = list;
@override
String toString() {
return a.join('|');
}
int get hashCode {
var bytes = utf8.encode(a.toString());
return md5.convert(bytes); a.toString().length;
//return a.toString().length; // silly hash
}
bool operator ==(other) {
String o = other.toString();
return (o == a.join('|'));
}
}
void main() {
A _checked = A(['1', '2', '3']);
A _other = A(['1', '2', '3']);
A _next = A(['12', '3']);
print(_checked == _other); // true
print(_checked); // 1|2|3
print(_checked == _next); // false
print(_next); // 12|3
}
Yes class is overkill, you can use just a function for the same:
String toString(List<String> a) {
return a.join('|');
}
print(toString(_checked) == toString(_other)); // true
print(toString(_checked)); // 1|2|3
print(toString(_checked) == toString(_next)); // false
print(toString(_next)); // 12|3