简体   繁体   中英

Flutter: RangeError (index): Invalid value: Valid value range is empty: 0


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]),
                      ),
                    ),
              ),
            ),
          ],
        )
    );
  }
}

when I pass there a list with just one item it shows me the error on the title, but then if I add more items in the list it displays them but always without the first and with the red error. Many other people got this error but I couldn't find a solution for mine. Any help would be appreciated.

Error Log:

════════ 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)

The error "RangeError (index): Invalid value: Valid value range is empty: 0" occurs when the List index that you're trying to access is empty or non-existent. Since there are two List being accessed in the class ( results and types ), make sure that both has the same length to avoid accessing out of range indexes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM