【问题标题】:StackedIndex is causing a failed assertion errorStackedIndex 导致失败的断言错误
【发布时间】:2020-10-19 04:24:58
【问题描述】:

我正在尝试创建一个带有底部导航视图的应用程序。我在其中一个索引上更改了 StreamBuilder 的格式,自从我更改了该 streamBuilder 后,我就遇到了一个奇怪的错误:

RenderBox 未布置:RenderRepaintBoundary#eae01 relayoutBoundary=up2 需要-PAINT 'package:flutter/src/rendering/box.dart':断言失败:第 1694 行 位置 12: 'hasSize'

我也得到了:

断言失败:第 1697 行第 12 行:'!_debugDoingThisLayout':不是 真的。

这是我认为导致问题的构建和初始化:

@override
  void initState() {
    super.initState();
    // TODO: implement initState
    _outerStream = Firestore.instance
        .collection('posts/player/post')
        .orderBy('time', descending: true)
        .snapshots()
        .take(2);

    _innerStream =
        Firestore.instance.collection('posts/player/post').snapshots();
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      child: StreamBuilder<QuerySnapshot>(
        stream: _outerStream,
        builder: (context, snapshot) {
          if (!snapshot.hasData) return Container(child: Text('Loading...'));
          final int highLightCount = snapshot.data.documents.length;
          return StreamBuilder<QuerySnapshot>(
            stream: _innerStream,
            builder: (context, snapshot) {
              if (!snapshot.hasData)
                return Container(child: Text('There are no current posts'));
              return ListView(
                physics: const AlwaysScrollableScrollPhysics(),
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                children: getPostItems(snapshot),
              );
            },
          );
        },
      ),
    );
  }

这段代码现在肯定是乱七八糟的,但下面是正在使用的其他方法:

getPostItems(AsyncSnapshot<QuerySnapshot> snapshot) {
    return snapshot.data.documents.map((doc) => getListItem(doc)).toList();
  }

  Widget getListItem(var doc) {
    getProfUrl(doc);
    getDownUrl(doc);

    VideoPlayerController _videoPlayerController;
    _videoPlayerController = VideoPlayerController.network(downUrl)
      ..initialize();
    _videoPlayerController.setLooping(true);
    _videoPlayerController.play();

    if (doc["user"] != widget.auth.getUserId()) {
      print("will show");
      if (doc["type"] == "image") {
        return new Column(
          children: <Widget>[
            new GestureDetector(
              onTap: () {
                Navigator.push(
                  context,
                  new MaterialPageRoute(
                      builder: (context) => new playerViewProfilePageIndex(
                          widget.auth, doc["user"])),
                );
              },
              child: new ListTile(
                title: new Text(doc["title"]),
                subtitle: new Text(doc["description"].toString()),
                leading: new Container(
                  width: 44.0,
                  height: 44.0,
                  decoration: new BoxDecoration(
                    shape: BoxShape.circle,
                    image: new DecorationImage(
                        fit: BoxFit.fill, image: NetworkImage(profUrl)),
                  ),
                ),
              ),
            ),
            new Padding(
              padding: EdgeInsets.fromLTRB(4, 4, 4, 4),
              child: new Center(
                child: new AspectRatio(
                  aspectRatio: 1 / 1,
                  child: new Container(
                    decoration: new BoxDecoration(
                        image: new DecorationImage(
                      fit: BoxFit.fill,
                      alignment: FractionalOffset.topCenter,
                      image: new NetworkImage(downUrl),
                    )),
                  ),
                ),
              ),
            ),
          ],
        );
      } else {
        return new Column(children: <Widget>[
          new ListTile(
              title: new Text(doc["title"]),
              subtitle: new Text(doc["description"].toString()),
              leading: new Container(
                  width: 44.0,
                  height: 44.0,
                  decoration: new BoxDecoration(
                    shape: BoxShape.circle,
                    image: new DecorationImage(
                        fit: BoxFit.fill, image: NetworkImage(profUrl)),
                  ))),
          new Padding(
            padding: EdgeInsets.fromLTRB(4, 4, 4, 4),
            child: new Center(
              child: new AspectRatio(
                aspectRatio: 500 / 500,
                child: VideoPlayer(_videoPlayerController),
              ),
            ),
          ),
        ]);
      }
    }
  }

【问题讨论】:

  • 我想 getPostItems(snapshot) 是一个返回 List 的方法,你能显示那个代码吗?
  • 我将代码编辑到原始问题中
  • 是的,我现在看到了,Column Widget 总是尝试扩展并填充父级的整个大小,但是父级(ListView)正在等待子级的大小,所以尝试使用 mainAxisSize 属性来MainAxisSize.min 或将其包装在具有特定大小的 sizedBox 中
  • 这种情况是从一开始就发生的,还是您首先可以看到“正在加载..”和“没有当前帖子”的文字?
  • 我刚刚尝试了这两种解决方案来包装列,SizedBox 或 MainAxisSize 似乎都不会影响应用程序。当我最初加载应用程序时,它会说加载一秒钟然后中断。到目前为止,它应该正在尝试加载一个锅。编辑!仔细一看,可以查看当前帖子的账号是成功的,但是没有帖子可见的账号是不成功的。谢谢你的帮助。

标签: flutter dart stream-builder


【解决方案1】:

getListItem 期望返回一个小部件,但是当 doc["user"] != widget.auth.getUserId() 为 false 时它永远不会返回值,也许添加一个带有 else 的虚拟 SizedBox.shrink 可能会有所帮助

Widget getListItem(var doc) {
    getProfUrl(doc);
    getDownUrl(doc);

    VideoPlayerController _videoPlayerController;
    _videoPlayerController = VideoPlayerController.network(downUrl)
      ..initialize();
    _videoPlayerController.setLooping(true);
    _videoPlayerController.play();

    if (doc["user"] != widget.auth.getUserId()) {
      print("will show");
      if (doc["type"] == "image") {
        return new Column(
          children: <Widget>[
            new GestureDetector(
              onTap: () {
                Navigator.push(
                  context,
                  new MaterialPageRoute(
                      builder: (context) => new playerViewProfilePageIndex(
                          widget.auth, doc["user"])),
                );
              },
              child: new ListTile(
                title: new Text(doc["title"]),
                subtitle: new Text(doc["description"].toString()),
                leading: new Container(
                  width: 44.0,
                  height: 44.0,
                  decoration: new BoxDecoration(
                    shape: BoxShape.circle,
                    image: new DecorationImage(
                        fit: BoxFit.fill, image: NetworkImage(profUrl)),
                  ),
                ),
              ),
            ),
            new Padding(
              padding: EdgeInsets.fromLTRB(4, 4, 4, 4),
              child: new Center(
                child: new AspectRatio(
                  aspectRatio: 1 / 1,
                  child: new Container(
                    decoration: new BoxDecoration(
                        image: new DecorationImage(
                      fit: BoxFit.fill,
                      alignment: FractionalOffset.topCenter,
                      image: new NetworkImage(downUrl),
                    )),
                  ),
                ),
              ),
            ),
          ],
        );
      } else {
        return new Column(children: <Widget>[
          new ListTile(
              title: new Text(doc["title"]),
              subtitle: new Text(doc["description"].toString()),
              leading: new Container(
                  width: 44.0,
                  height: 44.0,
                  decoration: new BoxDecoration(
                    shape: BoxShape.circle,
                    image: new DecorationImage(
                        fit: BoxFit.fill, image: NetworkImage(profUrl)),
                  ))),
          new Padding(
            padding: EdgeInsets.fromLTRB(4, 4, 4, 4),
            child: new Center(
              child: new AspectRatio(
                aspectRatio: 500 / 500,
                child: VideoPlayer(_videoPlayerController),
              ),
            ),
          ),
        ]);
      }
    } else return const SizedBox.shrink(); //return a Widget when the if is false
  }

【讨论】:

    猜你喜欢
    • 2018-07-30
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2013-08-17
    • 2021-10-11
    相关资源
    最近更新 更多