flutter - StackedIndex导致失败的断言错误

标签 flutter dart stream-builder

我正在尝试使用底部导航 View 创建应用程序。自从更改streamBuilder以来,我就更改了其中一个索引的StreamBuilder格式,但我一直收到一个奇怪的错误:

RenderBox was not laid out: RenderRepaintBoundary#eae01 relayoutBoundary=up2 NEEDS-PAINT 'package:flutter/src/rendering/box.dart': Failed assertion: line 1694 pos 12: 'hasSize'


我也得到:

Failed assertion: line 1697 pos 12: '!_debugDoingThisLayout': is not true.


这是我认为导致问题的生成和初始化:
@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),
              ),
            ),
          ),
        ]);
      }
    }
  }

最佳答案

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

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
  }

关于flutter - StackedIndex导致失败的断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62630802/

相关文章:

flutter - 使用 flutter 开发不同的屏幕尺寸?

多次调用 Flutter 构建

list - Flutter:我如何在列表中实现列表?错误:只能在initializers.dart中访问静态成员(implicit_this_reference_in_initializer)

flutter - 我如何控制头像大小和它与FilterChip中的标签之间的间距

dart - Flutter-手机屏幕关闭时如何播放音频

flutter - 让 StreamBuilder 中的 Stream 仅运行一次

json - Flutter HTTP获取JSON数据

flutter : How to use Riverpod with SharedPreference and List<String> Variable in multipage?

sorting - 按日期排序arraylist

firebase - 在Flutter中从Firebase FireCloud接收数据时,不会执行StreamBuilder中的快照代码