Hello friends i am beginner in flutter and my problem is that when my listview sacreen is load i want jump to specific item in list view i am using scrollable_positioned_list package problem is that when sacreen is loaded i am getting this error
Null check operator used on a null value here is my code
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'Dbhelper.dart';
class Allverse extends StatefulWidget {
late int booknumber;
late int chapternumber;
@override
_AllverseState createState() => _AllverseState();
Allverse(this.booknumber, this.chapternumber);
}
class _AllverseState extends State<Allverse> {
final ItemScrollController itemScrollController = ItemScrollController();
@override
void initState() {
// TODO: implement initState
super.initState();
//this method load listview all item
getallverse();
//here when item load i want jump to postion 10 in item
itemScrollController.jumpTo(index: 10);
}
List<Map<String, dynamic>> allverse = [];
void getallverse() async {
final data = await Dbhelper.fetchallverse(widget.booknumber, widget.chapternumber);
setState(() {
allverse= data;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('All verse'),
),
body:SingleChildScrollView(
child: Column(
children: [
Container(
height: MediaQuery.of(context).size.height,
child: ScrollablePositionedList.builder(
itemCount: allverse.length,
itemScrollController: itemScrollController,
itemBuilder:(context,index){
return GestureDetector(
onTap:(){
print(index);
},
child: Card(
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.lightBlue,
//backgroundColor:Colors.primaries[Random().nextInt(Colors.primaries.length)] ,
radius: 12,
child: Text(allverse[index]['v'].toString(),style: TextStyle(
color: Colors.white,
fontSize:12
),),
),
title: Column(
children: [
Text(allverse[index]['t'].toString(),
style: GoogleFonts.roboto(
fontSize: 16,
)
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(
Icons.favorite_border,
color: Colors.red[500],
),
Icon(
Icons.speaker,
color: Colors.red[500],
),
],
)
],
),
trailing: Icon(
Icons.share,
color: Colors.red,
),
),
),
);
},
),
),
],
),
)
);
}
}
flutter: Opening existing database
flutter: Opening existing database
======== Exception caught by widgets library =======================================================
The following _CastError was thrown building Builder:
Null check operator used on a null value
The relevant error-causing widget was:
MaterialApp MaterialApp:file:///Users/apple/Documents/bibleapp/lib/main.dart:14:12
When the exception was thrown, this was the stack:
#0 ItemScrollController.jumpTo (package:scrollable_positioned_list/src/scrollable_positioned_list.dart:206:25)
#1 _AllverseState.initState (package:bibleapp/Allverse.dart:30:26)
#2 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4728:57)
#3 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4561:5)
... Normal element mounting (24 frames)
#27 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3631:14)
#28 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6261:36)
#29 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6272:32)
... Normal element mounting (173 frames)
#202 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3631:14)
#203 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6261:36)
#204 Element.updateChild (package:flutter/src/widgets/framework.dart:3383:18)
#205 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5684:32)
#206 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6284:17)
#207 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#208 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#209 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#210 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#211 StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#212 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#213 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#214 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#215 ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#216 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#217 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#218 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#219 ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#220 _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:111:11)
#221 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#222 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#223 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#224 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#225 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#226 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#227 StatefulElement.update (package:flutter/src/widgets/framework.dart:4795:5)
#228 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#229 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#230 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#231 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6130:14)
#232 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#233 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#234 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#235 ProxyElement.update (package:flutter/src/widgets/framework.dart:4943:5)
#236 Element.updateChild (package:flutter/src/widgets/framework.dart:3370:15)
#237 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4613:16)
#238 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#239 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#240 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2578:33)
#241 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#242 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#243 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1145:15)
#244 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#245 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:996:5)
#249 _invoke (dart:ui/hooks.dart:150:10)
#250 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#251 _drawFrame (dart:ui/hooks.dart:114:31)
(elided 3 frames from dart:async)
====================================================================================================
CodePudding user response:
I think you did not give a value to one of these two variables
late int booknumber; late int chapternumber;
check the widget you are calling Allverse() from it
CodePudding user response:
I think you can achieve this by putting the controller.jump into Future.delay after 1 to 2 minute or what you put the time . when your list has been updated then your controller.jump run and it will not give you null
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'Dbhelper.dart';
class Allverse extends StatefulWidget {
late int booknumber;
late int chapternumber;
@override
_AllverseState createState() => _AllverseState();
Allverse(this.booknumber, this.chapternumber);
}
class _AllverseState extends State<Allverse> {
final ItemScrollController itemScrollController = ItemScrollController();
@override
void initState() {
// TODO: implement initState
super.initState();
//this method load listview all item
getallverse();
Future.delayed(const Duration(minute: 1), () {
setState(() {
//here when item load i want jump to postion 10 in item
itemScrollController.jumpTo(index: 10);
});
});
}
List<Map<String, dynamic>> allverse = [];
void getallverse() async {
final data = await Dbhelper.fetchallverse(widget.booknumber, widget.chapternumber);
setState(() {
allverse= data;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('All verse'),
),
body:SingleChildScrollView(
child: Column(
children: [
Container(
height: MediaQuery.of(context).size.height,
child: ScrollablePositionedList.builder(
itemCount: allverse.length,
itemScrollController: itemScrollController,
itemBuilder:(context,index){
return GestureDetector(
onTap:(){
print(index);
},
child: Card(
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.lightBlue,
//backgroundColor:Colors.primaries[Random().nextInt(Colors.primaries.length)] ,
radius: 12,
child: Text(allverse[index]['v'].toString(),style: TextStyle(
color: Colors.white,
fontSize:12
),),
),
title: Column(
children: [
Text(allverse[index]['t'].toString(),
style: GoogleFonts.roboto(
fontSize: 16,
)
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(
Icons.favorite_border,
color: Colors.red[500],
),
Icon(
Icons.speaker,
color: Colors.red[500],
),
],
)
],
),
trailing: Icon(
Icons.share,
color: Colors.red,
),
),
),
);
},
),
),
],
),
)
);
}
}
if my answer will help you please mark my answer as accepted