-1

I've created a class with 2 arguments, and 'title' is non-nullable and 'leading' is nullable.
So I defined two possible cases with if statement, for when 'leading' is null and not null.
(I refered this article: Understanding null safety | Dart)

But on VS Code, following error is continuously displayed:

The element type 'Icon?' can't be assigned to the list type 'Widget'. (dartlist_element_type_not_assignable)

What should I do to treat a null safety problem on this situation?

class MyPageListTile extends StatelessWidget {
  final Text title;
  final Icon? leading;

  MyPageListTile({this.leading, required this.title});

  @override
  Widget build(BuildContext context) {
    if (leading is Icon) {
      return Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [leading, SizedBox(width: 10), title]);
    } else {
      return Row(
          crossAxisAlignment: CrossAxisAlignment.start, children: [title]);
    }
  }
}
  • 1
    What is the problem with checking `leading != null` ? If the issue persists inside the `if` try adding a bang operator (!) to title. – esentis Jul 15 '22 at 07:22
  • 1
    @esentis Ok, I haven't been known about bang operator and why it exists in dart. Just now I googled about it, applied it on my code, and it works. Thanks a lot! XD – i.meant.to.be Jul 15 '22 at 07:40

1 Answers1

1

replace

final Icon? leading;

with

final Widget? leading;
Ravin Laheri
  • 801
  • 3
  • 11