0

How do I convert List into a String

How do I then use _checked string value in other place

  List<String> _checked = [];

2
  • I cannot seem to understand how do I implement this in my scenario Commented Jul 18, 2021 at 8:06
  • maybe you need to explain more on your question. How you going to use the String value? Commented Jul 18, 2021 at 8:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The instance member checked cannot be accessed in an initializer var checked=_checked.toString();
@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.
@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.
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

7 Comments

Please write your answer in English.
@Daniil The instance member checked cannot be accessed in an initializer var checked=_checked.toString()
_checked.toString() do you need it?
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, yes, I add an another solution as a funtion
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.