RangeError (end): Invalid value: Not in inclusive range 0..17: 18

Viewed 32

I know there are alot of similar questions in the platoform but i just cant find similar soulitions to mine.

So my listview.builder in my streambuilder is working fine when I do not update the details of an item. But when i access the item detail page and update my item using a function i created in the item detail page. The details will get updated but when i return to the item list page with listview.builder in streambuilder. The error occurs ("RangeError (end): Invalid value: Not in inclusive range 0..17: 18"). I am not sure why this happens. Please help me.

This is my streambuilder page

  Widget build(BuildContext context) {
    return StreamProvider<List <Announcement>>.value(
      value: DatabaseService(uid: ' ', docID: ' ', sort: '${isOldToNew}',aLevel: widget.selectionLevel,aType: widget.selectionType).announcementList,
      initialData: [],
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Theme.of(context).primaryColor,
          elevation: 0.0,
          centerTitle: true,
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: AnnouncementListForAll(),
            ),
          ],
        ),
      ),
    );
  }

My listview.builder is in the AnnouncementListForAll class in the expanded widget. Below is my AnnouncementListForAll code.

Widget build(BuildContext context) {

    final announcementList = Provider.of<List <Announcement>>(context);

    return Scrollbar(
      child: ListView.builder(
        scrollDirection: Axis.vertical,
        shrinkWrap: true,
        itemCount: announcementList.length,
        itemBuilder: (context, index){
          return announcementTileForAll(announcementlist: announcementList[index]);
        },
      ),
    );
  }

This is my tile class in the listview.builder

class announcementTileForAll extends StatelessWidget {

  final Announcement announcementlist;
  announcementTileForAll({ required this.announcementlist });

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 8.0,
      margin: new EdgeInsets.symmetric(
          horizontal: 10.0, vertical: 6.0),
      child: Container(
        decoration: BoxDecoration(
            color: Color.fromRGBO(64, 75, 96, .5)),
        child: ListTile(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => announcementDetailForAll(announcementlist.id),
                ),
              );
            },
            contentPadding: EdgeInsets.symmetric(
                horizontal: 10.0, vertical: 10.0
            ),
            title: Text(
              announcementlist.title.length > 15 ? '${announcementlist.title.substring (0,18)} ...' : announcementlist.title, // limit the words that will be displayed out on screen
              style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold),
            ),
            // subtitle: Text("10.07.2020", style: TextStyle(color: Colors.white)),

            subtitle: Column(
              children: <Widget>[
                Align(
                  alignment: Alignment.centerLeft,
                  child: Padding(
                    padding: const EdgeInsets.fromLTRB(0.0,2.0,0.0,0.0),
                    child: Text(announcementlist.content.length > 20 ? '${announcementlist.content.substring (0,20)} ... ' : announcementlist.content), // limit the words that will be displayed out on screen
                  ),
                ),
                Row(
                  children: <Widget>[
                    Icon(Icons.linear_scale,
                        color: Colors.yellowAccent),
                    Text(announcementlist.dateTime,
                        style: TextStyle(color: Colors.white)),
                  ],
                ),
              ],
            ),
            trailing: Icon(Icons.keyboard_arrow_right,
                color: Colors.white, size: 30.0)),
      ),
    );
  }
}

Apparently i found out it is the substring issue, how can i solve this problem ?

0 Answers
Related