Home > Software design >  How to fix error: A value of type Iterable can't be assigned to a variable of type 'List&l
How to fix error: A value of type Iterable can't be assigned to a variable of type 'List&l

Time:11-16

I am trying to make filter of List of countries but I get the error with types from IDE.

This is the message of the error:

A value of type 'Iterable' can't be assigned to a variable of type 'List'. Try changing the type of the variable, or casting the right-hand type to 'List'.

This is my widget:

const List<String> countriesList = <String>["Aphganistan",...];
//This countriesList comes from constants/countries.dart

class SignUp extends StatefulWidget {
  const SignUp({super.key});

  @override
  State<SignUp> createState() => _SignUpState();
}

class _SignUpState extends State<SignUp> {
  static List<String> _allCountries = countriesList;
  static List _foundCountries = [];

  @override
  void initState() {
    _foundCountries = _allCountries;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextFormField(
        onChanged: (String enteredKeyword) {
          List<dynamic> results = [];
          if (enteredKeyword.isEmpty) {
            results = _allCountries;
          } else {
            results = _allCountries.where((element) => false); // Here i get this error!
          }
          setState(() {
            _foundCountries = results;
          });
          log(enteredKeyword);
        },
      ),
    );
  }
}

I tried to change List to Iterable but I get error when I try to get an element in ListView.builder => _foundCountries[index] // it returns an error.

CodePudding user response:

You need to change your iterable to a list, like this:

results = _allCountries.where((element) => false).toList();//<- change

Which returns a list of data...

  • Related