在最新版本的 flutter 上使用 Carousel 时,getter 'key' 被调用为 null
The getter 'key' was called on null while using Carousel on latest version of flutter
将 flutter 更新到最新版本后,我在 Carousel
上遇到了这个异常。 Carousel
在 flutter 版本 1.22.4 上运行良好。但是在切换到最新版本 (2.0.3) 后,我遇到了这个异常。下面是代码和异常。我正在使用下面的包
https://pub.dev/packages/carousel_pro
异常
════════ Exception caught by rendering library
═════════════════════════════════
The following NoSuchMethodError was thrown during performLayout():
The getter 'key' was called on null.
Receiver: null
Tried calling: key
The relevant error-causing widget was Carousel
lib\…\homeViews\homeAppBarDelegate.dart:39 When the exception was
thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 SliverChildListDelegate.build package:flutter/…/widgets/sliver.dart:722
#2 SliverMultiBoxAdaptorElement._build package:flutter/…/widgets/sliver.dart:1201
#3 SliverMultiBoxAdaptorElement.createChild. package:flutter/…/widgets/sliver.dart:1214
#4 BuildOwner.buildScope package:flutter/…/widgets/framework.dart:2647 ... The following
RenderObject was being processed when the exception was fired:
RenderSliverFillViewport#4a87d relayoutBoundary=up2 NEEDS-LAYOUT
NEEDS-PAINT RenderObject: RenderSliverFillViewport#4a87d
relayoutBoundary=up2 NEEDS-LAYOUT NEEDS-PAINT
parentData: paintOffset=Offset(0.0, 0.0) (can use size)
constraints: SliverConstraints(AxisDirection.right, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: 0.0,
remainingPaintExtent: 411.4, crossAxisExtent: 230.0,
crossAxisDirection: AxisDirection.down, viewportMainAxisExtent: 411.4,
remainingCacheExtent: 411.4, cacheOrigin: 0.0)
geometry: null
no children current live ════════════════════════════════════════════════════════════════════════════════
代码
class HomeView extends StatefulWidget {
@override
_HomeViewState createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
@override
Widget build(BuildContext context) {
return Scaffold(
...................
Stack(
children: [
CustomScrollView(
controller: _scrollController,
slivers: <Widget>[
SliverPersistentHeader(
delegate: HomeAppBarDelegate(
maxExtent: 230,
minExtent: 50,
),
pinned: true,
floating: false,
),
],
),
],
),
),
},
}
import 'package:carousel_pro/carousel_pro.dart';
class HomeAppBarDelegate extends SliverPersistentHeaderDelegate {
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return Stack(
children: [
...................
// ------------------------------ C O V E R B L U R I M A G E S
Carousel(
animationCurve: Curves.easeInOutQuint,
animationDuration: Duration(seconds: 1),
dotBgColor: Colors.transparent,
overlayShadow: true,
autoplayDuration: Duration(seconds: 4),
showIndicator: false,
dotIncreasedColor: Colors.brown,
onImageChange: (prevInd, currInd) => coverImgIndex = currInd,
images: [
...coverImagesList.map(
(singleImg) => HomeBlurCoverImg(
imgURl: singleImg,
animationVal: animationVal,
maxExtent: maxExtent,
sizingInformation: sizingInformation,
),
),
],
),
],
),
出现黑屏
错误是 geometry: null no children current live
。
您的小部件未收到任何 children,您能否确认这不是问题所在?在 运行 小部件之前填充您的项目,或者在呈现它之前使用 isNotEmpty
检查,这应该可以解决您的问题。
具体来说 coverImagesList
,我看不到您将此列表的值传递到哪里,或者您的小部件树从哪里获取它。
将 flutter 更新到最新版本后,我在 Carousel
上遇到了这个异常。 Carousel
在 flutter 版本 1.22.4 上运行良好。但是在切换到最新版本 (2.0.3) 后,我遇到了这个异常。下面是代码和异常。我正在使用下面的包
https://pub.dev/packages/carousel_pro
异常
════════ Exception caught by rendering library ═════════════════════════════════ The following NoSuchMethodError was thrown during performLayout(): The getter 'key' was called on null. Receiver: null Tried calling: key
The relevant error-causing widget was Carousel lib\…\homeViews\homeAppBarDelegate.dart:39 When the exception was thrown, this was the stack #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 SliverChildListDelegate.build package:flutter/…/widgets/sliver.dart:722 #2 SliverMultiBoxAdaptorElement._build package:flutter/…/widgets/sliver.dart:1201 #3 SliverMultiBoxAdaptorElement.createChild. package:flutter/…/widgets/sliver.dart:1214 #4 BuildOwner.buildScope package:flutter/…/widgets/framework.dart:2647 ... The following RenderObject was being processed when the exception was fired: RenderSliverFillViewport#4a87d relayoutBoundary=up2 NEEDS-LAYOUT NEEDS-PAINT RenderObject: RenderSliverFillViewport#4a87d relayoutBoundary=up2 NEEDS-LAYOUT NEEDS-PAINT parentData: paintOffset=Offset(0.0, 0.0) (can use size) constraints: SliverConstraints(AxisDirection.right, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: 0.0, remainingPaintExtent: 411.4, crossAxisExtent: 230.0, crossAxisDirection: AxisDirection.down, viewportMainAxisExtent: 411.4, remainingCacheExtent: 411.4, cacheOrigin: 0.0) geometry: null no children current live ════════════════════════════════════════════════════════════════════════════════
代码
class HomeView extends StatefulWidget {
@override
_HomeViewState createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
@override
Widget build(BuildContext context) {
return Scaffold(
...................
Stack(
children: [
CustomScrollView(
controller: _scrollController,
slivers: <Widget>[
SliverPersistentHeader(
delegate: HomeAppBarDelegate(
maxExtent: 230,
minExtent: 50,
),
pinned: true,
floating: false,
),
],
),
],
),
),
},
}
import 'package:carousel_pro/carousel_pro.dart';
class HomeAppBarDelegate extends SliverPersistentHeaderDelegate {
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return Stack(
children: [
...................
// ------------------------------ C O V E R B L U R I M A G E S
Carousel(
animationCurve: Curves.easeInOutQuint,
animationDuration: Duration(seconds: 1),
dotBgColor: Colors.transparent,
overlayShadow: true,
autoplayDuration: Duration(seconds: 4),
showIndicator: false,
dotIncreasedColor: Colors.brown,
onImageChange: (prevInd, currInd) => coverImgIndex = currInd,
images: [
...coverImagesList.map(
(singleImg) => HomeBlurCoverImg(
imgURl: singleImg,
animationVal: animationVal,
maxExtent: maxExtent,
sizingInformation: sizingInformation,
),
),
],
),
],
),
出现黑屏
错误是 geometry: null no children current live
。
您的小部件未收到任何 children,您能否确认这不是问题所在?在 运行 小部件之前填充您的项目,或者在呈现它之前使用 isNotEmpty
检查,这应该可以解决您的问题。
具体来说 coverImagesList
,我看不到您将此列表的值传递到哪里,或者您的小部件树从哪里获取它。