OneCompiler

Flutter - error in conditional widget

I am trying to add a widget based on a condition like following

  if (post != null)
  {
    Container(
        margin: EdgeInsets.only(top: MySize.size4),
        child: Text("${post.title}",
            style: AppTheme.getTextStyle(
                themeData.textTheme.subtitle1,
                fontWeight: 600,
                letterSpacing: 0))),
  }

I am getting the following error with this code

The element type 'Set<Container>' can't be assigned to the list type 'Widget'.dartlist_element_type_not_assignable

1 Answer

5 years ago by

Remove the curly braces for if condition.
Change it to the following

if (post != null)
Container(
  margin: EdgeInsets.only(top: MySize.size4),
  child: Text("${post.title}",
      style: AppTheme.getTextStyle(
          themeData.textTheme.subtitle1,
          fontWeight: 600,
          letterSpacing: 0))),
5 years ago by Karthik Divi