Listview使应用程序Flutter崩溃

时间:2020-05-03 14:49:46

标签: flutter dart

当我在列表视图小部件或应用中注释一行时,它可以工作,但是当我取消注释时,它根本不会出现...

这是一个列表视图,用于呈现您通过api接收到的通知列表,api在未来的构建器中正常运行,但是当我添加listbuilder时,它将停止工作...

Image without listview Image with listview

代码


import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:mais_mob/src/shared/models/notifications_model.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:transparent_image/transparent_image.dart';

class ProfilePage extends StatefulWidget {
  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  String _search;
  int _page = 0;
  Future<List<NotificationModel>> _getGifs() async {
    http.Response response;

    response = await http.get(
        "http://127.0.0.1:8001/api/v1/notifications/get/f331dfd0-cae4-410d-9700-b9ec72c48d7c");
    List myModels;
    myModels = (json.decode(response.body) as List)
        .map((i) => NotificationModel.fromJson(i))
        .toList();

    return myModels;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: _future());
  }

  _future() {
    return FutureBuilder(
        future: _getGifs(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return SingleChildScrollView(
              child: Column(
                // mainAxisAlignment: MainAxisAlignment.start,
                // crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Stack(
                    children: <Widget>[
                      Container(
                        width: double.infinity,
                        height: 280.0,
                        decoration: BoxDecoration(
                            color: Color.fromRGBO(162, 56, 0, 1),
                            borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(30.0),
                                bottomRight: Radius.circular(30.0))),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 60.0, left: 20.0),
                        child: InkWell(
                            onTap: () {
                              Navigator.of(context).pop();
                            },
                            child: Icon(
                              Icons.arrow_back,
                              color: Colors.white,
                            )),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 150.0),
                        child: Center(
                          child: Container(
                            height: 200.0,
                            width: 310.0,
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius:
                                    BorderRadius.all(Radius.circular(20.0)),
                                boxShadow: [
                                  BoxShadow(
                                      color: Colors.black12.withOpacity(0.1)),
                                ]),
                            child: Column(
                              children: <Widget>[
                                Padding(
                                  padding: const EdgeInsets.only(top: 20.0),
                                  child: Container(
                                    height: 100.0,
                                    width: 100.0,
                                    decoration: BoxDecoration(
                                        image: DecorationImage(
                                            image: AssetImage(
                                                "assets/image/profile/profile3.jpg"),
                                            fit: BoxFit.cover),
                                        color: Colors.white,
                                        borderRadius: BorderRadius.all(
                                          Radius.circular(50.0),
                                        ),
                                        boxShadow: [
                                          BoxShadow(
                                              color: Colors.black12
                                                  .withOpacity(0.2),
                                              blurRadius: 10.0,
                                              spreadRadius: 2.0)
                                        ]),
                                  ),
                                ),
                                SizedBox(
                                  height: 5.0,
                                ),
                                Text(
                                  "user['name']",
                                  style: TextStyle(
                                      color: Colors.black,
                                      fontFamily: "Sofia",
                                      fontWeight: FontWeight.w700,
                                      fontSize: 20.0),
                                ),
                                Text(
                                  "user['email']",
                                  style: TextStyle(
                                      color: Colors.black38,
                                      fontFamily: "Sofia",
                                      fontWeight: FontWeight.w300,
                                      fontSize: 16.0),
                                ),
                              ],
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                  Padding(
                    padding: const EdgeInsets.only(
                        left: 25.0, top: 40.0, bottom: 10.0),
                    child: Text(
                      "Notificações",
                      style: TextStyle(
                          fontFamily: "Sofia",
                          fontWeight: FontWeight.w700,
                          fontSize: 16.0),
                    ),
                  ),
                  // ListView.builder(
                  //     itemCount: 1,
                  //     itemBuilder: (context, index) {
                  //       return ListTile(
                  //         title: Text(snapshot.data[index].id),
                  //         subtitle: Text('x'),
                  //       );
                  //     }),
                  SizedBox(
                    height: 20.0,
                  )
                ],
              ),
            );
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        });
  }
}

日志终端

颤振:渲染库引起的异常CA ═════════════════════ flutter:在performResize()期间引发了以下断言: 颤动:赋予垂直视口无限的高度。 颤动:视口沿滚动方向扩展以填充其容器。在这种情况下,垂直 抖动:视口被赋予了无限的垂直空间,可以在其中进行扩展。这个情况 颤动:通常在将可滚动小部件嵌套在另一个可滚动小部件内时发生。 flutter:如果此窗口小部件始终嵌套在可滚动窗口小部件中,则无需使用视口,因为 颤动:孩子们总是会有足够的垂直空间。在这种情况下,请考虑使用列 扑:相反。否则,请考虑使用“ shrinkWrap”属性(或ShrinkWrappingViewport)调整大小 抖动:视口的高度与其子代高度的总和。 扑: 扑:相关的引起错误的小部件是: 颤抖:ListView文件:///Users/lucas/www1/mais_mob/lib/src/pages/dashboard/profile.dart:149:28 扑: 扑:引发异常时,这是堆栈: 颤动:#0 RenderViewport.performResize。 (包:flutter / src / rendering / viewport.dart:1172:15) 颤动:#1 RenderViewport.performResize(package:flutter / src / rendering / viewport.dart:1233:6) 颤动:#2 RenderObject.layout(包:flutter / src / rendering / object.dart:1703:9) 颤抖:#3 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#4 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#5 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#6 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#7 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#8 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#9 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#10 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#11 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#12 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#13 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤动:#14 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤动:#15 RenderFlex.performLayout(包:flutter / src / rendering / flex.dart:744:15) 颤振:#16 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#17 _RenderSingleChildViewport.performLayout(package:flutter / src / widgets / single_child_scroll_view.dart:497:13) 颤抖:#18 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#19 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#20 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#21 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#22 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#23 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤动:#24 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#25 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#26 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#27 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#28 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#29 RenderProxyBoxMixin.performLayout(package:flutter / src / rendering / proxy_box.dart:105:13) 颤振:#30 RenderObject.layout(包:flutter / src / rendering / object.dart:1724:7) 颤抖:#31 MultiChildLayoutDelegate.layoutChild(包:flutter / src / rendering / custom_layout.dart:163:11) 颤动:#32 _ScaffoldLayout.performLayout(package:flutter / src / material / scaffold.dart:477:7) 颤动:#33 MultiChildLayoutDelegate._callPerformLayout(package:flutter / src / rendering / custom_layout.dart:232:7) 颤振:#34 RenderCustomMultiChildLayoutBox.performLayout(package:flutter / src / rendering / custom_layout.dart:391:14) 颤振:#35 RenderObject._layoutWithoutResize(package:flutter / src / rendering / object.dart:1584:7) 颤动:#36 PipelineOwner.flushLayout(包:flutter / src / rendering / object.dart:844:18) 颤动:#37 RendererBinding.drawFrame(包:flutter / src / rendering / binding.dart:344:19) 颤抖:#38 WidgetsBinding.drawFrame(包:flutter / src / widgets / binding.dart:774:13) 颤抖:#39 RendererBinding._handlePersistentFrameCallback(包:flutter / src / rendering / binding.dart:283:5) 颤抖:#40 SchedulerBinding._invokeFrameCallback(包:flutter / src / scheduler / binding.dart:1102:15) 颤抖:#41 SchedulerBinding.handleDrawFrame(包:flutter / src / scheduler / binding.dart:1041:9) 颤抖:#42 SchedulerBinding._handleDrawFrame(包:flutter / src / scheduler / binding.dart:957:5) 扑:#46 _invoke(dart:ui / hooks.dart:259:10) 扑:#47 _drawFrame(dart:ui / hooks.dart:217:3) 抖动:(从dart:async包中删除了3帧) 扑: 颤动:引发异常时,正在处理以下RenderObject:RenderViewport#7ca7e NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE: 颤动:需要合成 颤振:创建者:视口←IgnorePointer- [GlobalKey#8349f]←语义←_PointerListener←Listener← 颤抖:_GestureSemantics←RawGestureDetector- [LabeledGlobalKey#91c65]← 颤振:_PointerListener←监听器←_ScrollableScope←_ScrollSemantics- [GlobalKey#fd66b]←可滚动← 颤动 颤动:parentData :(可以使用大小) 颤动:约束:BoxConstraints(0.0 <= w <= 414.0,0.0 <= h <= Infinity) 扑动:大小:MISSING 颤动:轴方向:下 颤振:crossAxisDirection:右 flutter:偏移量:ScrollPositionWithSingleContext#09283(偏移量:0.0,范围:null..null,视口:null, flutter:ScrollableState,AlwaysScrollableScrollPhysics-> BouncingScrollPhysics,IdleScrollActivity#87e45, 颤振:ScrollDirection.idle) 颤动:锚定:0.0 颤动:此RenderObject具有以下子孙(显示到深度5): 颤振:中心子元素:RenderSliv​​erPadding#68fac需要布局的需要油漆需要组成的位更新 颤振:子级:RenderSliv​​erList#0a754 NEEDS-LAYOUT NEEDS-PAINT 颤动══════════════════════════════════════════════════ ══ 颤抖:引发了另一个异常:未布置RenderBox:RenderViewport#7ca7e NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderViewport#7ca7e NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderIgnorePointer#26c9e relayoutBoundary = up14 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤动:引发了另一个异常:未布置RenderBox:RenderSemanticsAnnotations#4973c relayoutBoundary = up13 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderPointerListener#cdd75 relayoutBoundary = up12 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderSemanticsGestureHandler#b1e5a relayoutBoundary = up11 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderPointerListener#18a2b relayoutBoundary = up10 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:_RenderScrollSemantics#39c4e relayoutBoundary = up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderFlex#041d2 relayoutBoundary = up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:_RenderSingleChildViewport#b5a7c relayoutBoundary = up7 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderIgnorePointer#9907c relayoutBoundary = up6 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderSemanticsAnnotations#9654a relayoutBoundary = up5 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderPointerListener#7ea85 relayoutBoundary = up4 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤动:引发了另一个异常:未布置RenderBox:RenderSemanticsGestureHandler#44215 relayoutBoundary = up3 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:RenderPointerListener#11392 relayoutBoundary = up2 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:_RenderScrollSemantics#c667c relayoutBoundary = up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 颤抖:引发了另一个异常:未布置RenderBox:_RenderSingleChildViewport#b5a7c relayoutBoundary = up7 NEEDS-PAINT 颤抖:引发了另一个异常:NoSuchMethodError:在null上调用了getter'visible'。

4 个答案:

答案 0 :(得分:1)

由于没有给ListView.builder一个高度,所以出现了错误。 您可以通过将ListView包装在Container中并为Container赋予heightwidth属性来解决此问题。

检查下面的代码,它可以正常工作:

Container(
      // give it your desired height here
      height: 500,
      // give it your desired height here
      width: MediaQuery.of(context).size.width,
      child: ListView.builder(
        itemCount: 1,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(snapshot.data[index].id),
            subtitle: Text('x'),
          );
        },
      ),
    );

我希望这会有所帮助。

答案 1 :(得分:1)

尝试在列中嵌套 Listview 或 ListView.builder 时可能会发生这种情况。

您可以使用 Expanded 放置 Listview 或 ListView.builder。

Expanded( 
    child: ListView()
)

您可以使用 SizedBox 放置 Listview 或 ListView.builder,并为 SizedBox 的“高度”参数设置一个值。高度将是固定的。

SizedBox(
    height: 150,
    child: ListView()
)

您可以将 ListView 的 shrinkWrap 属性设置为 true。

ListView(
    shrinkWrap: true
)

答案 2 :(得分:0)

您尚未提供引发此错误的高度。您可以按照建议将其包装在容器中,如bu @Random Guru推荐,或者将其添加为Expanded的子代。

Expanded(
 child:ListView.Builder(...)
)

答案 3 :(得分:0)

这是因为listview的高度是无限的。如果添加了wrapwrap,它将只显示列表的足够高度。

如果无法解决问题,您可能也使用了Flexible()

添加收缩包装= true

Listview.builder( .... shrinkWrap:true. ),

如果仍然无法解决,请将Listview或包含它的小部件包装到Flexible中。

Flexible(flex:1,child :list/widget containing list)