【问题标题】:FLUTTER: RangeError (index): Index out of range: no indices are valid: 0 (Pass Parameters)FLUTTER:RangeError(索引):索引超出范围:没有有效索引:0(通过参数)
【发布时间】:2023-03-17 21:30:01
【问题描述】:

我希望你能帮助我找到解决这个问题的方法,我正在尝试将数据从列表和索引传递到下一个选项卡。

但我收到此错误: RangeError (index): Index out of range: no index is valid: 0

我留下我使用的代码。

所以我得到数据并填写列表:

 Future<List> getData() async {
    final response = await http.get(url);
    return json.decode(response.body);
  }

body: FutureBuilder<List>(
        future: getData(),
        builder: (context, snapshot) {
          if (snapshot.hasError) print(snapshot.error);
          return snapshot.hasData
              ? ItemList(
                  list: snapshot.data ?? [],
                )
              : new Center(
                  child: CircularProgressIndicator(),
                );
        },
      )

所以我尝试获取索引和列表以及错误指向我的位置:

  class ItemList extends StatelessWidget {
  
  const ItemList({Key? key, required this.list}) : super(key: key);

  final List list;

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: list.isEmpty ? 0 : list.length,
      itemBuilder: (context, i) {
        return Container(
          padding: const EdgeInsets.all(10.0),
          child: GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => Detail(list: list, index: i),//<----errror
                ),
              );
            },

我的另一个标签是这样的:

class Detail extends StatefulWidget {
  List list = [];
  int index = 0;
  Detail({index, list});

  @override
  _DetailState createState() => _DetailState();
}

我通过以下方式恢复它:

widget.list[widget.index]['id']

json

[{" id ":" 1 "," nombre de usuario ":" Victor "," contraseña ":" 12345 "," nivel ":" admin "}, {" id ":"5"," nombre de usuario ":" sekia "," contraseña ":" 12345 "," nivel ":" cliente "}]

【问题讨论】:

    标签: flutter flutter-web flutter-futurebuilder flutter-listview


    【解决方案1】:

    问题来自于您初始化Detail 页面的方式

    Detail({index, list});
    

    你需要在初始化中添加this,像这样:

    Detail({this.index, this.list});
    

    我认为应该可以解决问题。

    【讨论】:

    • 谢谢你,非常感谢你解决了我的问题:)
    猜你喜欢
    • 2020-07-16
    • 2020-12-02
    • 1970-01-01
    • 2012-04-07
    • 2022-12-23
    • 2020-12-28
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多