ListView.builder无法正常工作

在尝试实现flutter-firebase时, ListView.builder 无法显示!

注意,当我尝试显示没有 ListView.builder 的第一个元素时,它工作正常,即错误仅在与此代码块相关的内容中:

return ListView.builder(
    itemCount: snapshot.data.documents.length,
    padding: const EdgeInsets.only(top: 10.0),
    itemExtent: 25.0,
    itemBuilder: (context, index) {
    DocumentSnapshot ds = snapshot.data.documents[index];
    return Text(" ${ds['name']} ${ds['vote']}");
});

我的完整代码是:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() => runApp(MyApp(
  textInput: Text("Text Widget"),
));

class MyApp extends StatefulWidget {
  final Widget textInput;
  MyApp({this.textInput});

  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  bool checkBoxValue = false;
  @override
  Widget build(BuildContext ctxt) {
    return new MaterialApp(
      home: SafeArea(
        child: Scaffold(
            body: new Center(
                child: new Column(
                  children: <Widget>[
                    widget.textInput,
                    Checkbox(
                        value: checkBoxValue,
                        onChanged: (bool newValue){
                          setState(() {
                            checkBoxValue = newValue;
                          });
                        }
                    ),
                    StreamBuilder(
                        stream: Firestore.instance.collection('baby').snapshots(),
                        builder: (context, snapshot) {
                          if (!snapshot.hasData) return const Text('Loading...');

                           //   DocumentSnapshot ds = snapshot.data.documents[0];
                           //   return new Text(" ${ds['name']} ${ds['vote']}");

                          // When i try to replace the above code by the below it fails!
                               return ListView.builder(
                                itemCount: snapshot.data.documents.length,
                                padding: const EdgeInsets.only(top: 10.0),
                                itemExtent: 25.0,
                                itemBuilder: (context, index) {
                                DocumentSnapshot ds = snapshot.data.documents[index];
                                return Text(" ${ds['name']} ${ds['vote']}");
                              });
                           // End of the ListView builder that fails!

                        }),
                  ],
                ))),
      ),
    );
  }
}

UPDATE

我得到的错误是:

进行热重装... I / flutter(9119):══╡通过渲染图书馆的例外情况╞══════════════════════════════════════════════════════════════════════════════════════════════ ════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ (9119):垂直视口被赋予无限高度 . I / flutter(9119):视口在滚动方向上展开以填充其容器 . 在这种情况下,垂直I / flutter(9119):视口被赋予了无限量的垂直空间,可以在其中展开 . 这种情况I / flutter(9119):通常在可滚动小部件嵌套在另一个可滚动小部件内时发生 . I / flutter(9119):如果这个小部件总是嵌套在一个可滚动的小部件中,则不需要使用视口,因为I / flutter(9119):总是有足够的垂直空间供孩子使用 . 在这种情况下,请考虑使用Column I / flutter(9119):而不是 . 否则,请考虑使用“shrinkWrap”属性(或ShrinkWrappingViewport)来调整I / flutter(9119)的大小:视口的高度为其子高度的总和 . I / flutter(9119):I / flutter(9119):当抛出异常时,这就是堆栈:I / flutter(9119):#0 RenderViewport.performResize . (包:flutter / src / rendering / viewport.dart:985:15)I / flutter(9119):#1 RenderViewport.performResize(包:flutter / src / rendering / viewport.dart:1038:6)I / flutter( 9119):#2 RenderObject.layout(包:flutter / src / rendering / object.dart:1555:9)I / flutter(9119):#3 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart: 109:13)I / flutter(9119):#4 RenderObject.layout(包:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#5 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#6 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#7 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#8 RenderObject.layout(package:flutter / src / rendering / ob ject.dart:1570:7)I / flutter(9119):#9 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#10 RenderObject.layout( package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#11 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119) ):#12 RenderObject.layout(包:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#13 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109 :13)I / flutter(9119):#14 RenderObject.layout(包:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#15 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src /rendering/proxy_box.dart:109:13)I / flutter(9119):#16 RenderObject.layout(p ackage:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#17 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119) ):#18 RenderObject.layout(包:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#19 RenderFlex.performLayout(包:flutter / src / rendering / flex.dart:738 :15)I / flutter(9119):#20 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#21 RenderPositionedBox.performLayout(package:flutter / src /rendering/shifted_box.dart:381:13)I / flutter(9119):#22 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#23 MultiChildLayoutDelegate .layoutChild(package:flutter / src / rendering / custom_layout.dart:141:11)I / flutter(9119):#24 _ScaffoldLayout.performLayout(package:flutter / src / material / scaffold.dart:399:7)I / flutter(9119):#25 MultiChildLayoutDelegate._callPerformLayout(packa ge:flutter / src / rendering / custom_layout.dart:211:7)I / flutter(9119):#26 RenderCustomMultiChildLayoutBox.performLayout(package:flutter / src / rendering / custom_layout.dart:355:14)I / flutter(9119) ):#27 RenderObject.layout(包:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#28 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109 :13)I / flutter(9119):#29 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#30 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13 )I / flutter(9119):#31 _RenderCustomClip.performLayout(package:flutter / src / rendering / proxy_box.dart:1143:11)I / flutter(9119):#32 RenderObject.layout(package:flutter / src / rendering /object.dart:1570:7)I / flutter(9119):#33 RenderPadding.performLayout(package:flutter / src / rendering / shifted_box.dart:199:11)I / flutter(9119):#34 RenderObject.layout (包:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#35 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter( 9119):#36 RenderObject.layout(包:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#37 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.pe rformLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#38 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter (9119):#39 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#40 RenderObject.layout(package:flutter / src / rendering / object.dart :1570:7)I / flutter(9119):#41 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#42 RenderObject.layout(package:flutter /src/rendering/object.dart:1570:7)I / flutter(9119):#43 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):# 44 RenderObject.layout(包:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#45 _RenderProxyBox&RenderBox&RenderObjectWithC hildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#46 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#47 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#48 RenderObject.layout(package:flutter / src / rendering / object .dart:1570:7)I / flutter(9119):#49 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#50 RenderOffstage.performLayout(package :flutter / src / rendering / proxy_box.dart:2831:13)I / flutter(9119):#51 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119) :#52 RenderStack.performLayout(package:flutter / src / rendering / stack.dart:520:15)I / flutter(9119):#53 RenderObject.layout(package:flutter / src / rendering / object.dart: 1570:7)I / flutter(9119):#54 __RenderTheatre&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#55 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#56 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#57 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#58 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119):#59 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#60 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box .dart:109:13)I / flutter(9119):#61 RenderObject.layout(package:fl utter / src / rendering / object.dart:1570:7)I / flutter(9119):#62 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13)I / flutter(9119): #63 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#64 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:109:13 )I / flutter(9119):#65 RenderObject.layout(package:flutter / src / rendering / object.dart:1570:7)I / flutter(9119):#66 RenderView.performLayout(package:flutter / src / rendering /view.dart:125:13)I / flutter(9119):#67 RenderObject._layoutWithoutResize(package:flutter / src / rendering / object.dart:1445:7)I / flutter(9119):#68 PipelineOwner.flushLayout (包:flutter / src / rendering / object.dart:709:18)I / flutter(9119):#69_WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding.drawFrame(包:扑/ SRC /再现/ binding.dart:270:19)的I /扑(9119):#70 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&WidgetsBinding.drawFrame(包:扑/ SRC /部件/ binding.dart:627:13)予/扑(9119):#71 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding._handlePersistentFrameCallback(包:扑/ SRC /再现/ binding.dart:208:5)I /扑(9119):#72 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback(包:扑/ SRC /调度/结合.dart:990:15)I / flutter(9119):#73 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame(package:flutter / src / scheduler / binding.dart:930:9)I / flutter(9119):#74 _WidgetsFlutter结合与BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame . (包:flutter / src / scheduler / binding.dart:751:7)I / flutter(9119):#76 _Timer._runTimers(dart:isolate / runtime / libtimer_impl.dart:382:19)I / flutter(9119) :#77 _Timer._handleMessage(dart:isolate / runtime / libtimer_impl.dart:416:5)I / flutter(9119):#78 RawReceivePortImpl.handleMessage(dart:isolate / runtime / libisolate_patch.dart:165:12)I / flutter(9119):(从包dart中删除一帧:async)I / flutter(9119):I / flutter(9119):在触发异常时正在处理以下RenderObject:I / flutter(9119):RenderViewport# a2532 NEEDS-LAYOUT NEEDS-PAINT I / flutter(9119):创作者:视口←_ScrollableScope←IgnorePointer- [GlobalKey#363cb]←语义←听众←I / flutter(9119): GestureSemantics←RawGestureDetector- [LabeledGlobalKey#08fb7]←I / flutter(9119): ExcludableScrollSemantics- [GlobalKey#8d2f7]←RepaintBoundary←CustomPaint←RepaintBoundary←I / flutter(9119):NotificationListener←⋯I / flutter(9119):parentDat a :(可以使用大小)I / flutter(9119):约束:BoxConstraints(0.0 <= w <= 384.0,0.0 <= h <=无穷大)I / flutter(9119):大小:MISSING I / flutter(9119) :axisDirection:down I / flutter(9119):crossAxisDirection:right I / flutter(9119):offset:ScrollPositionWithSingleContext#45c18(offset:0.0,range:null..null,viewport:null,I / flutter(9119):ScrollableState ,AlwaysScrollableScrollPhysics - > ClampingScrollPhysics,IdleScrollActivity#d36b9,I / flutter(9119):ScrollDirection.idle)I / flutter(9119):anchor:0.0 I / flutter(9119):此RenderObject具有以下后代(显示深度为5) ):I / flutter(9119):RenderSliverPadding#9ca2b NEEDS-LAYOUT NEEDS-PAINT I / flutter(9119):RenderSliverFixedExtentList#1cb4d NEEDS-LAYOUT NEEDS-PAINT I / flutter(9119):RenderRepaintBoundary#8c403 NEEDS-LAYOUT NEEDS-PAINT I / flutter(9119):RenderParagraph#c05e3 NEEDS-LAYOUT NEEDS-PAINT I / flutter(9119):════════════════════════════════════════════════════════════════════════════════════════════ ═ ══════════════════════════════════════════════════ ══/ futter(9119):抛出了另一个例外:RenderBox没有布局:RenderViewport#a2532 NEEDS-LAYOUT NEEDS-PAINT I / flutter(9119):抛出了另一个异常:RenderBox没有布局:RenderViewport#a2532 NEEDS-PAINT I / flutter(9119):抛出了另一个异常:RenderBox没有布局:RenderIgnorePointer#cfad6 relayoutBoundary = up10 NEEDS-PAINT I / flutter(9119):抛出了另一个异常:RenderBox未布局:RenderSemanticsAnnotations#2fd15 relayoutBoundary = up9 NEEDS-PAINT I / flutter(9119):抛出另一个异常:RenderBox未布局:RenderPointerListener#0da92 relayoutBoundary = up8 NEEDS-PAINT I / flutter(9119):抛出了另一个异常:RenderBox没有布局:RenderSemanticsGestureHandler#588c9 relayoutBoundary = up7 NEEDS-PAINT I / flutter(9119):抛出了另一个异常:RenderBox没有布局:_RenderExcludableScrollSemantics#ad008 relayoutBoundary = up6 NEEDS-PAINT I / flutter(9119):抛出了另一个异常:RenderBox没有布局:RenderRepaintBoundary# 1e512 relayoutBoundary = up5 NEEDS-PAINT I / flutter(9119):抛出另一个异常:RenderBox未布局:RenderCustomPaint#ab136 relayoutBoundary = up4 NEEDS-PAINT I / flutter(9119):抛出了另一个异常:RenderBox未被放置out:RenderRepaintBoundary#e6025 relayoutBoundary = up3 NEEDS-PAINT在1,363ms中重新加载了394个库中的1个 . I / flutter(9119):抛出了另一个异常:RenderBox没有布局:RenderRepaintBoundary#e6025 relayoutBoundary = up3 NEEDS-PAINT

回答(2)

3 years ago

我找到了真正的问题,它是在:

return ListView.builder(
         itemExtent: 25.0 . // this made the limitation
)

我应该删除 itemExtent ,所以默认情况下列表变得可扩展:)

3 years ago

StreamBuilder 包装成 Expanded . 因为它在 Column