Home > Enterprise >  Flutter: Return list of Strings using single string in a specific format
Flutter: Return list of Strings using single string in a specific format

Time:03-11

I need to upload array of string to firestore. I need to return list of Strings using single string as input.

If the input is 'Firestore'. I should return list like this: [f,fi,fir,firs,first,firsto,firstor,firestore]

If the input is 'Google Cloud'. I should return list like this: [g, go, goo, goo, goog, googl, google, c, cl, clo, clou, cloud]

CodePudding user response:

For having [g, go, goo, goo, goog, googl, google, c, cl, clo, clou, cloud] as result

use this :

  String test = 'Google Cloud';
  List<String> chars = [];

  for (var word in test.split(" ")) {
    for (var i = 0; i <= word.length; i  ) {
      if (word.substring(0, i).isNotEmpty) {
        chars.add(word.substring(0, i));
      }
    }
  }

  print(chars);

CodePudding user response:

it's a good practice to share what you have already done. Here is a simple way of achieving what you want (excluding white spaces) :

  String test = 'firestore';
  List<String> chars = [];

  for (var i = 0; i <= test.length; i  ) {
    if (test.substring(0, i).isNotEmpty) {
      chars.add(test.substring(0, i));
    }
  }
  print(chars);

The above prints [f, fi, fir, fire, fires, firest, firesto, firestor, firestore]

  • Related