'package”flutter/src/widgets/framework.dart':Assert失败:'child == _child':不是真的

insrf1ej  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(483)
class Home extends StatelessWidget{

  @override
  Widget build(BuildContext context){
    return new MediaQuery(
      data: new MediaQueryData.fromWindow(ui.window),
      child: new SafeArea(
        child: new Material(
          child: new CustomScrollView(
            slivers: <Widget>[
              new SliverPersistentHeader(
                delegate: MyCustomAppBar(expandedHeight: 200),
                pinned: true,
              ),
              new SliverList(
                delegate: SliverChildBuilderDelegate(
                  (_,index) => ListTile(
                    title: Text(
                      "Index: $index",
                      textDirection: TextDirection.ltr,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

我正在创建一个自定义滚动视图,但我有错误,我不能理解它是从哪里生成的?
我正在使用SliverPersistentHeaderDelegate类创建自定义appBar。

1u4esq0p

1u4esq0p1#

错误

flutter/src/widgets/framework.dart' : Failed assertion: 'child == _child': is not true

表示Widget build需要Widget,但未返回有效Widget。如果Home类是main.dart中应用程序的入口点,那么在使用其他Widget之前,您可能需要先使用MaterialApp。

void main() {
  runApp(const Home());
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Home',
      home: // Call your Home Screen here.
    );
  }
}

相关问题