FLUTTER: RangeError (index): Index out of range: no indices are valid: 0 (传递参数)
FLUTTER: RangeError (index): Index out of range: no indices are valid: 0 (Pass Parameters)
我希望你能帮我想办法解决这个出现的问题,我正在尝试将列表和索引中的数据传递到下一个选项卡。
但我收到此错误:RangeError (index): Index out of range: no indices are 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"}]
问题出在您初始化 Detail
页面的方式上
Detail({index, list});
你需要在初始化中加入this
,像这样:
Detail({this.index, this.list});
我相信应该可以解决问题。
我希望你能帮我想办法解决这个出现的问题,我正在尝试将列表和索引中的数据传递到下一个选项卡。
但我收到此错误:RangeError (index): Index out of range: no indices are 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"}]
问题出在您初始化 Detail
页面的方式上
Detail({index, list});
你需要在初始化中加入this
,像这样:
Detail({this.index, this.list});
我相信应该可以解决问题。