flutter - 断言失败:第 556 行 pos 15:'scrollOffsetCorrection != 0.0':不正确
问题描述
将颤振升级到最新版本后。我正面临这个问题,对于另一个具有早期版本颤振的应用程序,我有相同的代码,它工作正常。
使用新的 ListView 添加两个或更多子项。
向下滚动列表到第一个孩子完全离开屏幕的位置。
一直向上滚动到初始位置。ListView 在屏幕上不显示任何内容(只是空白区域)。
附加最少的可重现代码:
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MYYApp(),
);
}
}
class MYYApp extends StatefulWidget {
@override
_MYYAppState createState() => _MYYAppState();
}
class _MYYAppState extends State<MYYApp> {
final list = [
'BMW',
'Fiat',
'Toyota',
'Fiat',
'Testa',
'Fiat',
'Ford',
'Fiat',
'BMW',
'Fiat',
'Toyota',
'Fiat',
'Testa',
'Fiat',
'Ford',
'Fiat'
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ListView.builder(
itemCount: list.length,
itemBuilder: (context,index){
return list[index]=='Fiat'? //list[index] == 'Fiat' (this condition check is responsible for the issue and earlier it was not an issue)
Container(
height: 300,
child: Center(child: Text(list[index])),
):Container();
})
),
);
}
}
这是错误:
════════ Exception caught by rendering library ═════════════════════════════════════════════════════
The method '-' was called on null.
Receiver: null
Tried calling: -(223.60756587000844)
The relevant error-causing widget was:
ListView file:///C:/Users/prave/AndroidStudioProjects/for_stackoverflow/lib/main.dart:49:25
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by rendering library ═════════════════════════════════════════════════════
The following assertion was thrown during performLayout():
'package:flutter/src/rendering/sliver.dart': Failed assertion: line 556 pos 15: 'scrollOffsetCorrection != 0.0': is not true.
Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
The relevant error-causing widget was:
ListView file:///C:/Users/prave/AndroidStudioProjects/for_stackoverflow/lib/main.dart:49:25
When the exception was thrown, this was the stack:
#2 new SliverGeometry (package:flutter/src/rendering/sliver.dart:556:15)
#3 RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:180:20)
#4 RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#5 RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:137:11)
#6 RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:377:11)
这只是错误的一部分,它会产生近 10 个相同类型的错误。
解决方案
一旦你给你的替代非菲亚特容器一个非零的高度,错误就会消失。
我不知道为什么会这样或者是否是故意的,但该列表似乎存在零高度元素的问题。
我建议您实际上对您的数据使用过滤机制,而不是通过在视图中将其设为零高度作为事后的想法来解决该部分。
推荐阅读
- mysql - 使用 MySQL DB 中的 Sqoop 连接多个表?
- tableau-api - Tableau - 使用产品对计算客户
- javascript - 如何制作一个像 HTML 标签一样接受值而不是从属性中获取值的 React 组件
- python - 如何为 mac 的 python 3.9 安装 pygame?
- git - 如何将共同作者添加到最新推送的 git commit?
- vue.js - Input v-checkbox 默认勾选
- reactjs - React Hooks,复制的数据不断改变原始源数据
- python - 如何拆分字符串并将所有拆分添加到一个长列中?
- linux - cgdb 向终端产生噪声输出符号
- javascript - 如何在 javascript 函数中设置 request.getSession().setAttribute(obj, obj)