尝试记录某些内容后 Flutter 小部件库崩溃了吗?

Flutter widgets library crashing after trying to log something?

我正在尝试创建一个电影详情应用程序,这是我目前页面的代码:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            expandedHeight: _appBarHeight,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              title: Text(movie.title),
              background: Stack(
                fit: StackFit.expand,
                children: <Widget>[
                  Hero(
                      tag: movie.id,
                      child: Image.network(
                        movie.posterArtUrl,
                        fit: BoxFit.cover,
                        height: _appBarHeight,
                      ))
                ],
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildListDelegate(<Widget>[
              RatingInfo(movie),
              _buildMovieDetails(_movieDetail),
              _buildMovieGenreChips(_movieDetail),
            ]),
          )
        ],
      ),
    );
  }

如您所见,此页面主要由两种方法提供支持,_buildMovieDetails()_buildMovieGenreChips()_buildMovieDetails() 方法工作得很好并且在 运行 时不会 return 出错。但是,当我 运行 使用 _buildMovieGenreChips() 方法时,问题就出现了。此方法如下所示:

Widget _buildMovieGenreChips(MovieDetail movieDetail) {
    if (_isLoading) {
      return Center(
        child: CircularProgressIndicator(),
      );
    } 
    else {
      print(movieDetail.genres.toString());
    }
  }
}

在这个时间点,该方法只是注销了 movieDetail.genres 的值,这是我创建的自定义流派对象的列表(由流派名称的字符串组成和 int 作为流派 ID)。但是,当此代码为 运行 时,我的应用程序崩溃并且以下堆栈跟踪为 returned:

I/flutter ( 9592): [Instance of 'Genre', Instance of 'Genre', Instance of 'Genre', Instance of 'Genre']
I/flutter ( 9592): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 9592): The following assertion was thrown building _ScrollableScope:
I/flutter ( 9592): 'package:flutter/src/widgets/sliver.dart': Failed assertion: line 291 pos 12: 'child != null': is
I/flutter ( 9592): not true.
I/flutter ( 9592):
I/flutter ( 9592): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 9592): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 9592): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 9592):   https://github.com/flutter/flutter/issues/new
I/flutter ( 9592):
I/flutter ( 9592): When the exception was thrown, this was the stack:
I/flutter ( 9592): #2      SliverChildListDelegate.build (package:flutter/src/widgets/sliver.dart)
I/flutter ( 9592): #3      SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:716:67)
I/flutter ( 9592): #4      _HashMap.putIfAbsent (dart:collection/runtime/libcollection_patch.dart:143:29)
I/flutter ( 9592): #5      SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:716:26)
I/flutter ( 9592): #6      SliverMultiBoxAdaptorElement.performRebuild (package:flutter/src/widgets/sliver.dart:702:69)
I/flutter ( 9592): #7      SliverMultiBoxAdaptorElement.update (package:flutter/src/widgets/sliver.dart:671:7)
I/flutter ( 9592): #8      Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #9      RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4371:32)
I/flutter ( 9592): #10     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4761:17)
I/flutter ( 9592): #11     _ViewportElement.update (package:flutter/src/widgets/viewport.dart:187:11)
I/flutter ( 9592): #12     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #13     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #14     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #15     ProxyElement.update (package:flutter/src/widgets/framework.dart:3901:5)
I/flutter ( 9592): #16     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #17     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #18     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #19     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #20     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #21     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #22     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #23     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #25     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #26     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #27     StatefulElement.update (package:flutter/src/widgets/framework.dart:3791:5)
I/flutter ( 9592): #28     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #29     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #30     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #31     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #32     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #33     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #34     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #35     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #36     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #37     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #38     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #39     StatelessElement.update (package:flutter/src/widgets/framework.dart:3694:5)
I/flutter ( 9592): #40     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #41     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #42     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #43     StatefulElement.update (package:flutter/src/widgets/framework.dart:3791:5)
I/flutter ( 9592): #44     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #45     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #46     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #47     StatefulElement.update (package:flutter/src/widgets/framework.dart:3791:5)
I/flutter ( 9592): #48     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #49     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #50     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #51     ProxyElement.update (package:flutter/src/widgets/framework.dart:3901:5)
I/flutter ( 9592): #52     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #53     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #54     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #55     StatelessElement.update (package:flutter/src/widgets/framework.dart:3694:5)
I/flutter ( 9592): #56     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #57     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #58     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #59     ProxyElement.update (package:flutter/src/widgets/framework.dart:3901:5)
I/flutter ( 9592): #60     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #61     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #62     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #63     ProxyElement.update (package:flutter/src/widgets/framework.dart:3901:5)
I/flutter ( 9592): #64     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #65     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4371:32)
I/flutter ( 9592): #66     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4761:17)
I/flutter ( 9592): #67     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #68     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #69     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #70     StatefulElement.update (package:flutter/src/widgets/framework.dart:3791:5)
I/flutter ( 9592): #71     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #72     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #73     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #74     ProxyElement.update (package:flutter/src/widgets/framework.dart:3901:5)
I/flutter ( 9592): #75     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #76     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #77     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #78     StatefulElement.update (package:flutter/src/widgets/framework.dart:3791:5)
I/flutter ( 9592): #79     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #80     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #81     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #82     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #83     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #84     StatelessElement.update (package:flutter/src/widgets/framework.dart:3694:5)
I/flutter ( 9592): #85     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #86     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4653:14)
I/flutter ( 9592): #87     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #88     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #89     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #90     StatefulElement.update (package:flutter/src/widgets/framework.dart:3791:5)
I/flutter ( 9592): #91     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #92     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #93     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #94     StatefulElement.update (package:flutter/src/widgets/framework.dart:3791:5)
I/flutter ( 9592): #95     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #96     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #97     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #98     ProxyElement.update (package:flutter/src/widgets/framework.dart:3901:5)
I/flutter ( 9592): #99     Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #100    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #101    Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #102    ProxyElement.update (package:flutter/src/widgets/framework.dart:3901:5)
I/flutter ( 9592): #103    Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #104    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #105    Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #106    StatefulElement.update (package:flutter/src/widgets/framework.dart:3791:5)
I/flutter ( 9592): #107    Element.updateChild (package:flutter/src/widgets/framework.dart:2691:15)
I/flutter ( 9592): #108    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
I/flutter ( 9592): #109    Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
I/flutter ( 9592): #110    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2234:33)
I/flutter ( 9592): #111    _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:626:20)
I/flutter ( 9592): #112    _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:208:5)
I/flutter ( 9592): #113    _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 9592): #114    _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter ( 9592): #115    _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter ( 9592): #116    _invoke (dart:ui/hooks.dart:120:13)
I/flutter ( 9592): #117    _drawFrame (dart:ui/hooks.dart:109:3)
I/flutter ( 9592): (elided 2 frames from class _AssertionError)
I/flutter ( 9592): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/zygote  ( 9592): Do partial code cache collection, code=30KB, data=26KB
I/zygote  ( 9592): After code cache collection, code=30KB, data=26KB
I/zygote  ( 9592): Increasing code cache capacity to 128KB
I/flutter ( 9592): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4031 pos 14: '_dependents.isEmpty': is not true.
I/flutter ( 9592): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 2232 pos 16: '!_dirtyElements[index]._active || _dirtyElements[index]._debugIsInScope(context)': is not true.

正如在堆栈跟踪的第一行中看到的,我的打印语句工作得很好,但我的应用程序在那之后就崩溃了,好像 Flutter 的小部件库崩溃了。有没有人 运行 以前处理过这个问题?

您的方法应该 return 一个 Widget,但是当 _isLoading 为假时,它 return 什么都没有。

试试这个。

Widget _buildMovieGenreChips(MovieDetail movieDetail) {
    if (_isLoading) {
      return Center(
        child: CircularProgressIndicator(),
      );
    } 
    else {
      print(movieDetail.genres.toString());
      return Text("some widget");
    }
  }
}