createState method

  1. @override
State<StatefulWidget> createState()
override

Creates the mutable state for this widget at a given location in the tree.

Subclasses should override this method to return a newly created instance of their associated State subclass:

@override
State<SomeWidget> createState() => _SomeWidgetState();

The framework can call this method multiple times over the lifetime of a StatefulWidget. For example, if the widget is inserted into the tree in multiple locations, the framework will create a separate State object for each location. Similarly, if the widget is removed from the tree and later inserted into the tree again, the framework will call createState again to create a fresh State object, simplifying the lifecycle of State objects.

Implementation

@override
State<StatefulWidget> createState() {
  List<Map<String, String>> jsonList = countryList;

  List<CountryCode> elements =
      jsonList.map((json) => CountryCode.fromJson(json)).toList();

  if (comparator != null) {
    elements.sort(comparator);
  }

  if (countryFilter != null && countryFilter!.isNotEmpty) {
    final uppercaseCustomList =
        countryFilter!.map((c) => c.toUpperCase()).toList();
    elements = elements
        .where((c) =>
            uppercaseCustomList.contains(c.code) ||
            uppercaseCustomList.contains(c.name) ||
            uppercaseCustomList.contains(c.dialCode))
        .toList();
  }

  return CountryCodePickerState(elements);
}