Flutter/Dart - Split Comma Separated String into 3 Variables?
You could use a Map
, which is better suited for variables with dynamic length :
final tagName = 'grubs, sheep';final split = tagName.split(',');final Map<int, String> values = { for (int i = 0; i < split.length; i++) i: split[i]};print(values); // {0: grubs, 1: sheep}final value1 = values[0];final value2 = values[1];final value3 = values[2];print(value1); // grubsprint(value2); // sheepprint(value3); // null
If you want just want to check if tagname is null just use tagname?.split()
. But then when you get the values you will have to do this as well because otherwise you'll get a null exception splitTag?[] ?? ""
The ?? is optional if you want to return a blank string instead of null.
you can use a list and add items one by one
final names= 'first, second';final splitNames= names.split(',');List splitList; for (int i = 0; i < split.length; i++){ splitList.add(splitNames[i]);}