我试图传递一个值(我的ListView.builder的当前索引号)给另一个函数。但我得到以下错误:
在构建BookFeederSearch(dirty,dependencies:)时抛出了以下RangeError:[_InheritedProviderScope<UserProvider?>],state:BookFeederSearchState#3217f):RangeError(index):无效值:有效值范围为空:0
请看我的代码:
class Search2 extends StatefulWidget {
static const String id = 'Search2';
const Search2({super.key});
@override
State<Search2> createState() => _Search2State();
}
class _Search2State extends State<Search2> {
bool _addToLibrary = false;
List _allResults = [];
List _resultList = [];
final TextEditingController _searchController = TextEditingController();
@override
void initState() {
_searchController.addListener(_onSearchChanged);
//getClientStream();
super.initState();
}
_onSearchChanged() {
print(_searchController.text);
searchResultList();
}
searchResultList() {
var showResults = [];
if (_searchController.text != "") {
for (var clientSnapShot in _allResults) {
var name = clientSnapShot['TitleEnglish'].toString().toLowerCase();
if (name.contains(_searchController.text.toLowerCase())) {
showResults.add(clientSnapShot);
}
}
} else {
showResults = List.from(_allResults);
}
setState(() {
_resultList = showResults;
});
}
getClientStream() async {
var data = await FirebaseFirestore.instance
.collection('Books')
.orderBy('TitleEnglish')
.get();
setState(() {
_allResults = data.docs;
});
searchResultList();
}
@override
void dispose() {
_searchController.removeListener(_onSearchChanged);
_searchController.dispose();
super.dispose();
}
@override
void didChangeDependencies() {
getClientStream();
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
final User? user = Provider.of<UserProvider>(context).getUser;
return Scaffold(
appBar: AppBar(
leadingWidth: 120,
backgroundColor: const Color(
(0XFF293D63),
),
leading: IconButton(
onPressed: () {
//print('Go Back Pressed');
Navigator.pop(context);
},
icon: SvgPicture.asset('images/button_goback_clean_svg.svg'),
),
title: const Text(
'SEARCH',
style: TextStyle(
fontFamily: 'Sigmar',
fontSize: 20.0,
),
),
centerTitle: true,
),
//below links to history_book.dart
//SingleChildScrollView avoids keyboard overflow
body: Column(
// crossAxisAlignment: CrossAxisAlignment.center,
// mainAxisSize: MainAxisSize.max,
// mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
height: 50,
width: 500,
padding: const EdgeInsets.symmetric(horizontal: 20),
decoration: BoxDecoration(
// color: Color(0xFF5F94AF),
color: Colors.blue[100],
// border: Border.all(width: 10, color: Colors.white),
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(30.0),
bottomRight: Radius.circular(30.0),
),
),
child: CupertinoSearchTextField(
//done with opacity so it has no colour
backgroundColor: Colors.white.withOpacity(0),
controller: _searchController,
placeholder: 'Search for book title',
style: const TextStyle(
fontFamily: 'NotoBold',
fontSize: 15.0,
color: Color((0XFF293D63)),
),
),
),
Expanded(
child: ListView.builder(
//scrollDirection: Axis.horizontal,
itemCount: _resultList.length,
itemBuilder: ((context, index) {
return Align(
alignment: Alignment.topCenter,
// this will have the folders overlap each other (set to 0.5)
heightFactor: 0.2,
child: Column(
children: [
Container(
height: 20,
),
Row(
children: [
Container(
width: 200,
),
Container(
width: 400,
height: 40,
decoration: BoxDecoration(
// color: Color(0xFF5F94AF),
color: Colors.blue[100],
// border: Border.all(width: 10, color: Colors.white),
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),
),
alignment: Alignment.centerLeft,
padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
child: InkWell(
onTap: () {
//To navigate to the next page (shows folder) and pass in the index numbers
final indexPage = index;
print('$indexPage, Open Folder');
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
BookFeederSearch(indexPage),
),
);
},
字符串
我不知道是什么导致了这个问题。我已经通过打印值进行了检查,但它确实得到了正确的输入。我以前给其他类传递过值,没有遇到过问题,但不是在ListView.builder中。所以我猜这是问题的原因,但找不到解决办法。非常感谢你的帮助。
2条答案
按热度按时间oymdgrw71#
错误消息指示有效值范围为空,表示要传递的索引值超出范围。
原因可能是您在使用
Navigator.of(context).push
推送路由时没有提供BookFeederSearch
小部件的密钥。尝试更新
onTap
处理程序,将index
作为key
传递给BookFeederSearch
小部件:字符串
同时更新
BookFeederSearch
小部件以接收indexPage
参数:型
jexiocij2#
您遇到的错误可能是由于您将
index
值传递给BookFeederSearch
构造函数的方式造成的。BuildContext
context
变量不应在ListView.builder
的itemBuilder
内部直接访问。相反,您可以使用Builder
小部件在itemBuilder
中创建一个新的BuildContext
。下面是如何修改代码来避免这个问题:
字符串
通过使用
Builder
小部件,您可以在itemBuilder
中创建一个新的BuildContext
,这将解决您面临的问题。导航到BookFeederSearch
屏幕时,确保使用innerContext
。