Custom Function in FlutterFlow Returns Empty List When Filtering Unique Strings

Custom Code

Hello, community! I'm facing an issue with a custom function in FlutterFlow. My goal is to create a function that takes a list of JSON elements, filters out empty strings, and returns a list of unique, non-empty strings
Here is the code I'm using for this function:

List<String>? filterUniqueStrings(List<dynamic>? elements) {

  if (elements == null || elements.isEmpty) {
    print("Input list is null or empty");
    return [];
  }

  print("Received elements: $elements");

  Set<String> uniqueStrings = {};

  for (var element in elements) {
    print("Processing element: $element");

    if (element is String) {
      print("Element is a string: $element");

      if (element.isNotEmpty) {
        print("Adding non-empty string: $element");
        uniqueStrings.add(element);
      } else {
        print("Skipping empty string");
      }
    } else {
      print("Skipping non-string element: $element");
    }
  }

  print("Unique strings: $uniqueStrings");

  return uniqueStrings.toList();

}

I tested this function with the following input: ["Hello", "", "World", "Hello", "Test", ""]

Expected output: ["Hello", "World", "Test"]
Actual output: [] (empty list)

The function is configured in FlutterFlow with these settings:

  • Input: JSON (List<JSON>, Nullable)

  • Output: List<String>, Nullable

What could be the reason for this behavior? Am I missing something in the way the function is configured or implemented? Any guidance would be greatly appreciated!"

What have you tried so far?

I have:

  • Ensured the function is properly defined in the FlutterFlow custom functions section.

  • Verified the input data and function logic.

  • Experimented with different variations of the function, such as checking for null or empty strings explicitly and using debugging logs.

  • Tested the function directly in FlutterFlow, but the output is always an empty list.

Any help from the community would be invaluable!

Did you check FlutterFlow's Documentation for this topic?
Yes