'Flutter how to convert String to List<String>
I have this String.
var String a = '["one", "two", "three", "four"]';
var ab = (a.split(','));
print(ab[0]); // return ["one"
I want to convert this to List<String>.
The problem is it returns square bracket too. I want to List looks this ["one", "two", "three", "four"] not [["one", "two", "three", "four"]].
How can I convert this properly?
Solution 1:[1]
Your string looks like a valid JSON, so this should work for you:
import 'dart:convert';
...
var a = '["one", "two", "three", "four"]';
var ab = json.decode(a);
print(ab[0]); // returns "one"
Solution 2:[2]
void main(){
String listA = '["one", "two", "three", "four"]';
var a = jsonDecode(listA);
print(a[0]); // print one
String listB = 'one,two,three,four';
var b = (listB.split(','));
print(b[0]); // print one
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | iDecode |
| Solution 2 | Dave |
