List<String> results = [];
class History extends StatefulWidget {
final bool isDarkMode;
final List<String> results;
final List<String> types;
History({this.isDarkMode, this.results, this.types});
_HistoryState createState() => _HistoryState(isDarkMode, results, types);
}
class _HistoryState extends State<History> {
_HistoryState(this.isDarkMode, this.results, this.types);
final bool isDarkMode;
final List<String> results;
final List<String> types;
@override
Widget build(BuildContext context) {
Color bgColor = this.isDarkMode ? Color(0xd91D201F) : Colors.white70;
Color appBarColor = this.isDarkMode ? Color(0xff1C1D21) : Color(0xd6E8E9F3);
Color iconColor = this.isDarkMode ? Colors.black87 : Color(0xe7A6A6A8);
Color textColor = this.isDarkMode ? Colors.grey[600] : Colors.black;
List reversed = results.reversed.toList();
return Scaffold(
backgroundColor: bgColor,
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: results.length,
itemBuilder: (context, index) =>
Card(
color: isDarkMode ? Colors.white10 : Colors.white,
child: ListTile(
leading: Text(reversed[index], style: TextStyle(
color: textColor,
fontWeight: FontWeight.bold,
fontSize: 50),),
trailing: Text(types[index]),
),
),
),
),
],
)
);
}
}
当我在其中传递仅包含一个项的列表时,它向我显示标题错误,但是如果我在列表中添加更多项,则显示它们,但始终没有没有第一并且出现红色错误。 许多其他人都收到了此错误,但我找不到我的解决方案。 任何帮助将不胜感激。
错误日志:
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following RangeError was thrown building:
RangeError (index): Invalid value: Valid value range is empty: 0
When the exception was thrown, this was the stack:
#0 List.[] (dart:core-patch/growable_array.dart:146:60)
#1 _HistoryState.build.<anonymous closure> (package:dices_app/history.dart:47:45)
#2 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:446:22)
#3 SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1134:67)
#4 _HashMap.putIfAbsent (dart:collection-patch/collection_patch.dart:139:29)
答案 0 :(得分:0)
当您尝试访问的列表索引为空或不存在时,会出现错误“RangeError (index): Invalid value: Valid value range is empty: 0”。由于类中有两个 List 被访问(results
和 types
),请确保两者具有相同的长度以避免访问超出范围的索引。