0

How do I convert List into a String

How do I then use _checked string value in other place

  List<String> _checked = [];
Rishail Siddiqui
  • 231
  • 1
  • 4
  • 13

2 Answers2

2

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.

Abion47
  • 22,211
  • 4
  • 65
  • 88
  • The instance member checked cannot be accessed in an initializer var checked=_checked.toString(); – Rishail Siddiqui Jul 18 '21 at 08:23
  • @RishailSiddiqui Show what you're doing. Is `checked` a normal variable? Is it a member variable? If it's a member variable, [it matters where you're trying to initialize it](https://stackoverflow.com/a/64548861/). – jamesdlin Jul 18 '21 at 08:45
  • @RishailSiddiqui It sounds like you're trying to do something in a constructor's initializer list. You won't be able to do that with these methods, so put the code in the constructor body. – Abion47 Jul 18 '21 at 08:59
1

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
Daniil Loban
  • 4,165
  • 1
  • 14
  • 20
  • Please write your answer in English. – John Joe Jul 18 '21 at 08:12
  • @Daniil The instance member checked cannot be accessed in an initializer var checked=_checked.toString() – Rishail Siddiqui Jul 18 '21 at 08:43
  • _checked.toString() do you need it? – Daniil Loban Jul 18 '21 at 08:48
  • The class method is such unbelievable overkill, and all it's doing is just calling `join` anyway. How is it better or even as good as just calling `join` in the first place? – Abion47 Jul 18 '21 at 17:46
  • @Abion47, yes, I add an another solution as a funtion – Daniil Loban Jul 18 '21 at 20:13
  • It's not good as an alternate solution. Yeah it works, but the way it works is the same way your first solution works and adds nothing other than increased complexity and unnecessary boilerplate. It's a strictly worse solution than the one you already had. – Abion47 Jul 18 '21 at 20:59
  • of course, that's why I can't understand why you were not satisfied with the first solution. what other solution do you expect? – Daniil Loban Jul 18 '21 at 22:16