gpt4 book ai didi

flutter - 当构建器获得大于 9 的值时,PageView 小部件中出现超出范围错误

转载 作者:行者123 更新时间:2023-12-04 10:03:08 24 4
gpt4 key购买 nike

数据长度为 10。
对于第 10 个值,我出现了超出范围的错误,而我的 Item 计数仍然大于 10。为什么会发生这种情况

import 'package:flutter/material.dart';
import 'dart:ui';
import 'package:momnts/landscape.dart';
import 'myCard.dart';
import 'data.dart';

class Homepage extends StatefulWidget {
@override
_HomepageState createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
var controller =PageController(initialPage: 0, viewportFraction: 0.8);
int currentPage = 0;
@override
void initState(){
controller.addListener((){
int next = controller.page.round();

if(currentPage!=next){
setState(() {
currentPage = next;
});
}
});
}

int totalLen = data.length+1;
@override
Widget build(BuildContext context) {
if (MediaQuery.of(context).orientation == Orientation.portrait) {
return Container(
color: Colors.white,
child: SafeArea(
child: Scaffold(
body: Container(
child: Column(
children: <Widget>[
Container(
height: 80.0,
child: Row(
children: <Widget>[
Expanded(
child: Text(
"Momnts",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
fontFamily: "Pacifico",
),
),
),
],
),
),
Expanded(
child: PageView.builder(
itemCount: totalLen+1,
itemBuilder: (context,int currentIdx){
if(currentIdx==0)
return SimplePage();
else if(currentIdx<10)
return MyCard(id:currentIdx,text:data[currentIdx],image: AssetImage('images/S$currentIdx.jpg'),name:name[currentIdx]);
else
return MyCard(id:currentIdx,text:data[currentIdx],image: AssetImage('images/S$currentIdx.jpg'),name:name[currentIdx]);
},
scrollDirection: Axis.horizontal,
controller: controller,
),
),
],
))),
),
);
} else
return Landscape();
}
}

class SimplePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: Row(children: <Widget>[
Expanded(
child: Text("Yes",
style: TextStyle(fontSize: 30.0,),),
),
CircleAvatar(child: Icon(Icons.arrow_right),)
],)
);
}
}

这是显示的错误
The following RangeError was thrown building:
I/flutter (16859): RangeError (index): Invalid value: Not in range 0..9, inclusive: 10
I/flutter (16859):
I/flutter (16859): When the exception was thrown, this was the stack:
I/flutter (16859): #0 List.[] (dart:core-patch/growable_array.dart:149:60)
I/flutter (16859): #1 _HomepageState.build.<anonymous closure> (package:momnts/homepage.dart:65:66)
I/flutter (16859): #2 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:446:15)**

最佳答案

如果您的 data.length是 10,那么您将传递 12 作为构建器的项目计数,我假设您应该有 10,所以 data.length .

int totalLen = data.length+1; //10+1


...



itemCount: totalLen+1, //10+1+1

关于flutter - 当构建器获得大于 9 的值时,PageView 小部件中出现超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61734613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com