1

I try to do a filter function for a Listview.

Here is the code of the filter function :

   void filterSearchResults() {
    List<Club> dummySearchList = List<Club>();
    dummySearchList.addAll(widget.club);
    if(currentEnvironment != '' ) {
      List<Club> dummyListData = List<Club>();
      dummySearchList.forEach((club) {
        if(club.environment == currentEnvironment) {
          dummyListData.add(club);
        }
      });
      setState(() {
        clubs.clear();
        clubs.addAll(dummyListData);
      });
      return;
    } else {
      setState(() {
        clubs.clear();
        clubs.addAll(widget.club);
      });
    }
  }

It's suposed to go through every Club I got, and if the environment of the Club is equal with the value of currentEnvironment, the club is added to a list for printing them out.

But the if(club.environment == currentEnvironment) always send me false. if I check both of my values, I found that the string of the current club object is "Mountain" and the currentEnvironment is "Mountain" too. They both supposed to be of the String type.

With if(club.environment == "Mountain") it's working as presumed.

I get my clubs with local Json, and the currentEnvironment is stored in SharedPreferences

Code to get my currentEnvironment :

class ClubList extends StatefulWidget {
  final List<Club> club;
  ClubList({Key key, this.club}) : super(key: key);

  @override
  _ClubListState createState() => _ClubListState();
}

class _ClubListState extends State<ClubList> {

  var clubs = List<Club>();
  String currentEnvironment;

  @override
  setData() {
    getSelectedEnvironment().then((value) {
      setState(() {
        currentEnvironment = value;
        print("currentEnvironment ${currentEnvironment}");
        filterSearchResults();
      });
    });
  }

  void initState() {
    clubs.addAll(widget.club);
    setData();
    filterSearchResults();
    super.initState();
  }

  Future<String> getSelectedEnvironment() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();

    return prefs.getString("selectedEnvironment");
  }

I don't know if you need other information, tell me if it's the case.

I found the solution to my problem. I just have to call the filterSearchResults() in my setData(). Like that, the function wait to have a value to be launched.

J. Quentin
  • 13
  • 6
  • have you try with this other way ? https://stackoverflow.com/questions/18428735/how-do-i-compare-two-objects-to-see-if-they-are-the-same-instance-in-dart – Amit Prajapati Sep 17 '19 at 15:55
  • try to remove ```@override``` from ```setData()``` so your code compiles correctly. – Ali Qanbari Sep 17 '19 at 16:17
  • What happens if you change the if statement to this: `if(club.environment.compareTo(currentEnvironment) == 0) ` – aytunch Sep 17 '19 at 17:44
  • Are you absolutely certain that both `String` instances have the same content? That is, with the way you print the strings, it would be very hard to tell if one of them has trailing whitespace. It also is conceivable that one has homoglyphs and only *looks* like "Mountain". – jamesdlin Sep 18 '19 at 05:37
  • @Jamesdlin I just checked the if statement in the `filterSearchResults` function with @AmitPrajapati solution. The result is false and by checking the value of `currentEnvironment`, it's null. I'm working on it now. thanks for both of your advices – J. Quentin Sep 18 '19 at 06:22
  • @aligator I edited my question with the future call that i make. And removing `@override` seems to do nothing. – J. Quentin Sep 18 '19 at 06:48
  • check if their lengths are equal – Ali Qanbari Sep 18 '19 at 07:51
  • I found a solution and edited my post. Thanks everyone. i wont forget the `identical(this, other)` function for dart ! – J. Quentin Sep 18 '19 at 08:00

0 Answers0