Flutter SliverList and CustomScrollView Error: 'owner.debugCurrentBuilderTarget == this' : is not true
Flutter SliverList and CustomScrollView Error: 'owner.debugCurrentBuilderTarget == this' : is not true
大家好,我正在尝试构建一个可重用的 SliverList,我只是将 sliver 小部件作为要显示的列表传递到其中,并加倍填充,但是目前出现此错误
我正在尝试构建的可重用小部件的代码
class ScrollScreen extends StatelessWidget {
ScrollScreen({
@required this.pageTitle,
@required this.pageImage,
@required this.widgets,
this.color = Colors.white,
});
final String pageTitle;
final String pageImage;
final List<Widget> widgets;
final Color color;
@override
Widget build(BuildContext context) {
return CustomScrollView(physics: const BouncingScrollPhysics(), slivers: [
SliverBar(title: pageTitle, image: pageImage),
SliverListLayout(widgets: widgets),
]);
}
Class 的 SliverListLayout
class SliverListLayout extends StatelessWidget {
final double paddingTB;
final double paddingLR;
final List<Widget> widgets;
SliverListLayout({
@required this.widgets,
this.paddingTB,
this.paddingLR,
});
@override
Widget build(BuildContext context) {
return SliverList(
delegate: SliverChildListDelegate([
...addPadding(widgets, paddingTB, paddingLR), //add padding wraps widgets in a padding widget and returns them,, not the issue works fine elsewhere
]),
);
}
}
堆栈跟踪:
The following NoSuchMethodError was thrown building
NotificationListener<KeepAliveNotification>:
The method '>=' was called on null.
Receiver: null
Tried calling: >=(0.0)
The relevant error-causing widget was
SliverList
lib\…\custom_widgets\sliverlist_layout.dart:15
When the exception was thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 EdgeInsetsGeometry.isNonNegative
package:flutter/…/painting/edge_insets.dart:52
#2 new RenderPadding
package:flutter/…/rendering/shifted_box.dart:107
#3 Padding.createRenderObject
package:flutter/…/widgets/basic.dart:1652
#4 RenderObjectElement.mount
颤动医生:
PS C:\Users\Usuario\Desktop\AppName> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Versión 10.0.19042.746], locale es-ES)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[!] Android Studio (version 4.1.0)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code (version 1.52.1)
[√] Connected device (1 available)
! Doctor found issues in 2 categories.
PS C:\Users\Usuario\Desktop\AppName>
添加填充
List<Widget> addPadding(List<Widget> widgets,
[double paddingTB = 25.0, double paddingLR = 25.0]) {
for (int i = 0; i < widgets.length; i++) {
widgets[i] = Padding(
padding: EdgeInsets.fromLTRB(
paddingLR,
paddingTB,
paddingLR,
paddingTB,
),
child: widgets[i],
);
}
return widgets;
}
我粘贴了 SliverListLayout class 中的代码(没有两个额外的参数),一切正常。还有 SliverBar returns 一个 SliverAppBar,它工作正常。非常感谢任何帮助。
我不确定如何重现您的问题。我会推荐:
- Post 一个完整但最小的代码示例重现您的问题
- Post 完整的控制台错误堆栈跟踪
- 提供有关您的配置的更多信息 (flutter doctor)
这是一个简单的代码示例,类似于(我认为)您尝试实现的目标。
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
body: ReusableSliverList(
title: 'Testing my ReusableSliverList',
widgetPadding: EdgeInsets.symmetric(horizontal: 32.0, vertical: 16.0),
widgets: List.generate(10, (index) => Text('Widget $index')).toList(),
)),
),
);
}
class ReusableSliverList extends StatelessWidget {
final String title;
final List<Widget> widgets;
final EdgeInsets widgetPadding;
const ReusableSliverList({
Key key,
this.title,
this.widgets,
this.widgetPadding,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: [
SliverAppBar(title: Text(title)),
SliverList(
delegate: SliverChildListDelegate(
widgets
.map((widget) => Padding(padding: widgetPadding, child: widget))
.toList(),
),
),
],
);
}
}
完整的工作代码基于你的问题编辑(使用addPadding):
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: ScrollScreen(
widgets: List.generate(10, (index) => Text('Widget $index')),
),
),
),
);
}
class ScrollScreen extends StatelessWidget {
ScrollScreen({
// @required this.pageTitle,
// @required this.pageImage,
@required this.widgets,
this.color = Colors.white,
});
// final String pageTitle;
// final String pageImage;
final List<Widget> widgets;
final Color color;
@override
Widget build(BuildContext context) {
return CustomScrollView(physics: const BouncingScrollPhysics(), slivers: [
// SliverBar(title: pageTitle, image: pageImage),
SliverListLayout(
widgets: widgets,
paddingLR: 16.0,
paddingTB: 32.0,
),
]);
}
}
class SliverListLayout extends StatelessWidget {
final double paddingTB;
final double paddingLR;
final List<Widget> widgets;
SliverListLayout({
@required this.widgets,
this.paddingTB,
this.paddingLR,
});
@override
Widget build(BuildContext context) {
return SliverList(
delegate: SliverChildListDelegate([
...addPadding(
widgets,
paddingTB,
paddingLR,
),
]),
);
}
List<Widget> addPadding(List<Widget> widgets,
[double paddingTB = 25.0, double paddingLR = 25.0]) {
for (int i = 0; i < widgets.length; i++) {
widgets[i] = Padding(
padding: EdgeInsets.fromLTRB(
paddingLR,
paddingTB,
paddingLR,
paddingTB,
),
child: widgets[i],
);
}
return widgets;
}
}
大家好,我正在尝试构建一个可重用的 SliverList,我只是将 sliver 小部件作为要显示的列表传递到其中,并加倍填充,但是目前出现此错误
我正在尝试构建的可重用小部件的代码
class ScrollScreen extends StatelessWidget {
ScrollScreen({
@required this.pageTitle,
@required this.pageImage,
@required this.widgets,
this.color = Colors.white,
});
final String pageTitle;
final String pageImage;
final List<Widget> widgets;
final Color color;
@override
Widget build(BuildContext context) {
return CustomScrollView(physics: const BouncingScrollPhysics(), slivers: [
SliverBar(title: pageTitle, image: pageImage),
SliverListLayout(widgets: widgets),
]);
}
Class 的 SliverListLayout
class SliverListLayout extends StatelessWidget {
final double paddingTB;
final double paddingLR;
final List<Widget> widgets;
SliverListLayout({
@required this.widgets,
this.paddingTB,
this.paddingLR,
});
@override
Widget build(BuildContext context) {
return SliverList(
delegate: SliverChildListDelegate([
...addPadding(widgets, paddingTB, paddingLR), //add padding wraps widgets in a padding widget and returns them,, not the issue works fine elsewhere
]),
);
}
} 堆栈跟踪:
The following NoSuchMethodError was thrown building
NotificationListener<KeepAliveNotification>:
The method '>=' was called on null.
Receiver: null
Tried calling: >=(0.0)
The relevant error-causing widget was
SliverList
lib\…\custom_widgets\sliverlist_layout.dart:15
When the exception was thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 EdgeInsetsGeometry.isNonNegative
package:flutter/…/painting/edge_insets.dart:52
#2 new RenderPadding
package:flutter/…/rendering/shifted_box.dart:107
#3 Padding.createRenderObject
package:flutter/…/widgets/basic.dart:1652
#4 RenderObjectElement.mount
颤动医生:
PS C:\Users\Usuario\Desktop\AppName> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Versión 10.0.19042.746], locale es-ES)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[!] Android Studio (version 4.1.0)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code (version 1.52.1)
[√] Connected device (1 available)
! Doctor found issues in 2 categories.
PS C:\Users\Usuario\Desktop\AppName>
添加填充
List<Widget> addPadding(List<Widget> widgets,
[double paddingTB = 25.0, double paddingLR = 25.0]) {
for (int i = 0; i < widgets.length; i++) {
widgets[i] = Padding(
padding: EdgeInsets.fromLTRB(
paddingLR,
paddingTB,
paddingLR,
paddingTB,
),
child: widgets[i],
);
}
return widgets;
}
我粘贴了 SliverListLayout class 中的代码(没有两个额外的参数),一切正常。还有 SliverBar returns 一个 SliverAppBar,它工作正常。非常感谢任何帮助。
我不确定如何重现您的问题。我会推荐:
- Post 一个完整但最小的代码示例重现您的问题
- Post 完整的控制台错误堆栈跟踪
- 提供有关您的配置的更多信息 (flutter doctor)
这是一个简单的代码示例,类似于(我认为)您尝试实现的目标。
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
body: ReusableSliverList(
title: 'Testing my ReusableSliverList',
widgetPadding: EdgeInsets.symmetric(horizontal: 32.0, vertical: 16.0),
widgets: List.generate(10, (index) => Text('Widget $index')).toList(),
)),
),
);
}
class ReusableSliverList extends StatelessWidget {
final String title;
final List<Widget> widgets;
final EdgeInsets widgetPadding;
const ReusableSliverList({
Key key,
this.title,
this.widgets,
this.widgetPadding,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: [
SliverAppBar(title: Text(title)),
SliverList(
delegate: SliverChildListDelegate(
widgets
.map((widget) => Padding(padding: widgetPadding, child: widget))
.toList(),
),
),
],
);
}
}
完整的工作代码基于你的问题编辑(使用addPadding):
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: ScrollScreen(
widgets: List.generate(10, (index) => Text('Widget $index')),
),
),
),
);
}
class ScrollScreen extends StatelessWidget {
ScrollScreen({
// @required this.pageTitle,
// @required this.pageImage,
@required this.widgets,
this.color = Colors.white,
});
// final String pageTitle;
// final String pageImage;
final List<Widget> widgets;
final Color color;
@override
Widget build(BuildContext context) {
return CustomScrollView(physics: const BouncingScrollPhysics(), slivers: [
// SliverBar(title: pageTitle, image: pageImage),
SliverListLayout(
widgets: widgets,
paddingLR: 16.0,
paddingTB: 32.0,
),
]);
}
}
class SliverListLayout extends StatelessWidget {
final double paddingTB;
final double paddingLR;
final List<Widget> widgets;
SliverListLayout({
@required this.widgets,
this.paddingTB,
this.paddingLR,
});
@override
Widget build(BuildContext context) {
return SliverList(
delegate: SliverChildListDelegate([
...addPadding(
widgets,
paddingTB,
paddingLR,
),
]),
);
}
List<Widget> addPadding(List<Widget> widgets,
[double paddingTB = 25.0, double paddingLR = 25.0]) {
for (int i = 0; i < widgets.length; i++) {
widgets[i] = Padding(
padding: EdgeInsets.fromLTRB(
paddingLR,
paddingTB,
paddingLR,
paddingTB,
),
child: widgets[i],
);
}
return widgets;
}
}