首页 > 解决方案 > Flutter 中的值不在范围 1 内

问题描述

我正在尝试使用列表视图列出我从 firebase 获取的数据,如您在此处看到的

  Widget chatRoomsList() {
    return StreamBuilder(
      stream: chatRooms,
      builder: (context, snapshot) {
        return snapshot.hasData
            ? ListView.builder(
                itemCount: snapshot.data.documents.length,
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  return ChatRoomsTile(
                    userName: snapshot.data.documents[index].data['chatRoomId']
                        .toString()
                        .replaceAll("_", "")
                        .replaceAll(Constants.myName, ""),
                    chatRoomId:
                        snapshot.data.documents[index].data["chatRoomId"],
                  );
                })
            : Container();
      },
    );
  }

但是,当我列出出现错误的项目时,您可以在此处看到:



════════ Exception caught by widgets library ═══════════════════════════════════
The following RangeError was thrown building ChatRoomsTile(dirty):
Value not in range: 1

The relevant error-causing widget was
ChatRoomsTile
package:charlotapp/…/chat/chat_room.dart:29
When the exception was thrown, this was the stack
#0      _StringBase.substring (dart:core-patch/string_patch.dart:399:7)
#1      ChatRoomsTile.build
package:charlotapp/…/chat/chat_room.dart:147
#2      StatelessElement.build
package:flutter/…/widgets/framework.dart:4641
#3      ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4567
#4      Element.rebuild
package:flutter/…/widgets/framework.dart:4261

知道为什么会这样吗?

标签: flutterlistview

解决方案


推荐阅读