参数类型 'Widget' 无法分配给参数类型 'PreferredSizeWidget?'

The argument type 'Widget' can't be assigned to the parameter type 'PreferredSizeWidget?'

class DetailChatPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Widget header() {
      return PreferredSize(
        preferredSize: Size.fromHeight(70),
        child: AppBar(
          backgroundColor: backgroundColor1,

        ),
      );
    }

    return Scaffold(
      backgroundColor: backgroundColor3,
      appBar: header(),
    );
  }
}

我的代码中出现错误 参数类型 'Widget' 不能设置为参数类型 'PreferredSizeWidget?' 如何解决这个问题

改变

Widget header() {
  return PreferredSize(
    preferredSize: Size.fromHeight(70),
    child: AppBar(
      backgroundColor: backgroundColor1,

    ),
  );
}

PreferredSize header() {
  return PreferredSize(
    preferredSize: Size.fromHeight(70),
    child: AppBar(
      backgroundColor: backgroundColor1,

    ),
  );
}

发生这种情况是因为您使用 Widget 类型声明了 header 方法。您应该改为使用 PreferredSizeWidget.

类型声明它
PreferredSizeWidget header() {
  return PreferredSize(
    preferredSize: Size.fromHeight(70),
    child: AppBar(
      backgroundColor: backgroundColor1,
    ),
  );
}