【问题标题】:Exception caught by widgets library RangeError (index): Invalid value: Not in inclusive range 0..35: 36小部件库 RangeError(索引)捕获的异常:无效值:不在包含范围内 0..35:36
【发布时间】:2021-09-13 06:55:03
【问题描述】:
Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
        title: Text(
          'Products',
         ),
       ),
    body: Center(
       child: FutureBuilder(
          future: fetchPhotos(),
               builder: (ctx, snapShot) {
                    if (snapShot.connectionState == ConnectionState.waiting) {
                         return CircularProgressIndicator();
                     } else {
                    return ListView.builder(
                      itemBuilder: (context, index) {
                         return ListTile(
                          leading: CircleAvatar(
                             backgroundColor: Colors.red,
                          ),
                           title: Text(snapShot.data["table"][index]["name"]),
                           subtitle: Text(
                              "price: ${snapShot.data["table"][index]["class_id"]}"),
                       );
                     },
                   );
                 }
               },
              ),
            ),
          );
         }

我应该在 itemCount 中添加什么以避免显示错误:小部件库 RangeError (index) 捕获的异常:无效值:不在包含范围 0..35:36 中

【问题讨论】:

    标签: flutter dart flutter-http


    【解决方案1】:

    itemCount 参数添加到您的listView Builder 中:

    ListView.builder(
     itemCount: snapShot.data["table"].length,
     itemBuilder: (context, index) {
           ...
    

    这样 Flutter 知道何时停止构建项目

    【讨论】:

    • 仅显示前 3 项
    • 您似乎正在显示表格对象,所以它将是itemCount: snapShot.data["table"].lenght
    • 错误:小部件库/类“列表”捕获的异常没有实例获取器“长度”。接收方:“_GrowableList”的实例(长度:36)尝试调用:长度
    • 这是length 而不是lenght,抱歉我拼错了。
    • 顺便说一句 @Jahidul 你也复制粘贴错了。
    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 2021-03-21
    • 2021-10-07
    • 2021-07-08
    • 2021-08-14
    • 2020-06-06
    • 2019-11-01
    • 2020-07-06
    相关资源
    最近更新 更多