Flutter:显示单个列表值时出错

Posted

技术标签:

【中文标题】Flutter:显示单个列表值时出错【英文标题】:Flutter: Error when displaying single list value 【发布时间】:2020-12-27 17:32:34 【问题描述】:

在 Flutter 中,我正在从磁盘读取文件并将列表项显示为列表。使用 ListView.builder 工作正常,但使用显示单个值的文本小部件时出现此错误。有人可以帮忙吗?

我得到的错误是 The following RangeError was throwed building MyHomePage(dirty, state: _MyHomePageState#e9932): RangeError(索引):无效值:有效值范围为空:9


 import 'package:flutter/material.dart';
 import 'dart:convert';
 import 'package:flutter/services.dart';
 import 'dart:async';
 
 void main() 
   runApp(MyApp());
 
 
 class MyApp extends StatelessWidget 
   //This widget is the root of your application.
   @override
   Widget build(BuildContext context) 
     return MaterialApp(
       title: 'Flutter Demo',
       theme: ThemeData(
         primarySwatch: Colors.blue,
         visualDensity: VisualDensity.adaptivePlatformDensity,
       ),
       home: MyHomePage(),
     );
   
 
 
 ______________________________
 
         WITH List.View.builder
 
 class MyHomePage extends StatefulWidget 
   @override
   _MyHomePageState createState() => _MyHomePageState();
 
 
 class _MyHomePageState extends State<MyHomePage> 
   List<String> _names = [];
 
   Future<List<String>> loadNames() async 
     List<String> names = [];
     await rootBundle.loadString('assets/Stulkur_A.txt').then((q) => 
           for (String i in LineSplitter().convert(q)) names.add(i)
         );
     return names;
   
 
   _setup() async 
     List<String> names = await loadNames();
     setState(() 
       _names = names;
     );
   
 
   @override
   void initState() 
     _setup();
     super.initState();
   
 
   @override
   Widget build(BuildContext context) 
     return Scaffold(
       appBar: AppBar(
         title: Text('Names'),
       ),
       body: Center(
         child: Container(
             padding: EdgeInsets.all(15),
             child: ListView.builder(
                 itemCount: _names.length,
                 itemBuilder: (context, index) 
                   return Text(_names[index]);
                 )),
       ),
     );
   
 
 
 _____________________
 
 WITH Text widget
 
 class MyHomePage extends StatefulWidget 
   @override
   _MyHomePageState createState() => _MyHomePageState();
 
 
 class _MyHomePageState extends State<MyHomePage> 
   List<String> _names = [];
 
   Future<List<String>> loadNames() async 
     List<String> names = [];
     await rootBundle.loadString('assets/Stulkur_A.txt').then((q) => 
           for (String i in LineSplitter().convert(q)) names.add(i)
         );
     return names;
   
 
   _setup() async 
     List<String> names = await loadNames();
     setState(() 
       _names = names;
     );
   
 
   @override
   void initState() 
     _setup();
     super.initState();
   
 
   @override
   Widget build(BuildContext context) 
     return Scaffold(
       appBar: AppBar(
         title: Text('Names'),
       ),
       body: Center(
         child: Container(
           padding: EdgeInsets.all(15),
           child: Text(_names[9]),
         ),
       ),
     );
   
 

【问题讨论】:

重启你的应用并重新调试,并提供给我们真实代码 【参考方案1】:

试试

body: Center(
        child: Container(
          padding: EdgeInsets.all(15),
          child: _names.isEmpty
              ? CircularProgressIndicator()
              : ListView.builder(
                  itemCount: _names.length,
                  itemBuilder: (context, index) 
                    return Text(_names[index]);
                  ,
                ),
        ),
      ),

【讨论】:

【参考方案2】:

您应该检查 _names.length == 0 是否显示加载程序或您想要的所有内容,否则显示 ListView 小部件。

【讨论】:

以上是关于Flutter:显示单个列表值时出错的主要内容,如果未能解决你的问题,请参考以下文章

Flutter - 在列表视图中仅选择单个项目

使用 showdialog 和 textfield 在 Flutter 中更新单个 Listview 项目

Flutter:列表视图显示空白或白屏?

将地图列表添加到 Firebase 时出错:Flutter

在 Flutter 中使用 moor 时使用关键字时出错

如何使用 bloc flutter 更改单个列表项的状态?