2

I am trying to run an if-else condition that displays certain Icons based on the given conditions. I am trying to see if a particular quizId,i.e. a string is present inside the list to fulfill the condition. For some reason, the condition doesn't seem to work even though the element is present inside the list.

I have tried manually comparing the quizId with a particular String, which worked out fine. But as soon as I bring the list into the condition, it stops working. I have found both the quizId and List element data types which is String yet they still can't be compared.

This is the piece of Code :

class QuizBadge extends StatelessWidget {
  const QuizBadge({super.key, required this.topic, required this.quizId});

  final Topic topic;
  final String quizId;

  @override
  Widget build(BuildContext context) {
    Report report = Provider.of<Report>(context);
    List completed = report.topics.values.toList();

    List a = completed.expand((element) => element).toList();
    print(a[1]);

    if (a[1] == "flutter-basics") {
      return const Icon(FontAwesomeIcons.checkDouble, color: Colors.green);
    } else {
      return const Icon(FontAwesomeIcons.solidCircle, color: Colors.grey);
    }
  }
}

Any help would be appreciated. Thank You.

Edited Code Piece :

  Widget build(BuildContext context) {
    Report report = Provider.of<Report>(context);

    List completed = report.topics[topic.id] ?? [];
    String str = completed.map((e) => e.toString()).join('-');

    if (str.contains(quizId)) {
      return const Icon(FontAwesomeIcons.checkDouble, color: Colors.green);
    } else {
      return const Icon(FontAwesomeIcons.solidCircle, color: Colors.grey);
    }
  }

This seems to be working.

1 Answer 1

1

This is occurring because the elements in your list are not strings. Consider checking the type returned by report.topics.values.toList() (I can't tell because you didn't include this) and/or explicitly declare the lists you expect to be strings as List<String>, for example:

    List<String> a = completed.expand((element) => element).toList();

You will see an error on that line if you declare it this way, and the error will offer a clue.

In general, I prefer to avoid using generics without specifying an explicit type, because the default is dynamic, and you don't get the benefit of static type checking. You can also maintain explicit typing by using var/final declarations, such as:

    var a = completed.expand((element) => element).toList();

Follow-Up

void main() {
  var completed = [["firestore-basics"], ["flutter-basics"], ["js-basics", "js-variables"], ["rxjs-basics"]];
  var a = completed.expand((element) => element).toList();
  print(a);
  bool contains = a.contains('flutter-basics');
  print('contains? = $contains');
}

Output:

[firestore-basics, flutter-basics, js-basics, js-variables, rxjs-basics]
contains? = true
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, this solved it. I converted the elements in the list to strings using map(). Thanks for the help!
One doubt tho. After reading your answer, I used List<String> a = completed.map((e) => e.toString()).toList() and then I placed a in the if condition but it doesn't seem to work even after the list is of the type List<String>. Any ideas for this?
What do you mean by "placed a in the if condition"? If you want to check if any element in the list equals the string, then you should use a.any((element) => element == "flutter-basics"). I'm not certain about what exactly it is you're trying to do, otherwise I could offer a more precise suggestion.
if (a .contains( "flutter-basics")) { return const Icon(FontAwesomeIcons.checkDouble, color: Colors.green); } else { return const Icon(FontAwesomeIcons.solidCircle, color: Colors.grey); } This is after making the variable a as a List<String>
Right, because that is a list of lists, not a list of strings, and string != [string]. Please see updated answer for a working example. Did you remove the expand()? I'm unclear what your code looks like now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.