Scaffold(
appBar: _buildAppBar(),
)
功能如下:
Widget _buildAppBar() => AppBar(); // Error
此代码运行良好,直到我将代码迁移到空安全。
答案 0 :(得分:0)
Dart null 安全不允许向下转换,因此您不能将 Widget
分配给 PreferredSizeWidget
,就像您不能将 Object
分配给 String
一样(这在空安全之前是可能的)。
您应该更改您的函数并从中返回 AppBar
或 PreferredSizeWidget
。
AppBar _buildAppBar() => AppBar();
或
PreferredSizeWidget _buildAppBar() => AppBar();
答案 1 :(得分:0)
我无法评论,但 iDecode 的答案是正确的
AppBar customAppBar(String title, bool centerTitle, List<Widget> actions) {}
这个错误是空的。