I don't know if you really need empty space in the list, but you can correct my example if you want to have spaces in the list.
void main() {
String text = 'Hello I am Gabriele!';
List<String> errList = ['Hello', 'I'];
// `#` uses as separator.
var a = text
.split(' ')
.map((e) => errList.contains(e) ? '$e#' : e)
.join(' ')
.split('#');
print(a); // [Hello, I, am Gabriele!]
print(a.join()); // Hello I am Gabriele!
}
According to your requirements:
void main() {
String text = 'Hello I am Gabriele!';
// Note: an empty space will be added after the objects except for the last one.
// Order is important if you want the right results.
List<String> errList = ['Hello', 'I'];
var result = text
.split(' ')
.map((e) => errList.contains(e) ? e + '#' : e)
.join(' ')
.split('#')
.expand((e) {
if (errList.contains(e.trim()) && !e.contains(errList.last)) return [e, ' '];
if (e.contains(errList.last)) return [e.trim()];
return [e];
}).toList();
print(result); // [Hello, , I, am Gabriele!]
}