Add and delete flutter bottom navigation bar (error is reported when adding)

When we want to add the bottom navigation bar, using insert will report an error:

The following RangeError was thrown building BottomNavigationBar(dirty, state:

_BottomNavigationBarState#a56dd(tickers: tracking 3 tickers)):

RangeError (index): Invalid value: Not in range 0..2, inclusive: 3

The solution is to:

bottomList = [
            //This way direct assignment is fine
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
            BottomNavigationBarItem(icon: Icon(Icons.add), label: "added"),
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "item"),
            BottomNavigationBarItem(icon: Icon(Icons. home), label: "My"),
          ];
replace
bottomList.insert(
              1, BottomNavigationBarItem(icon: Icon(Icons.add), label: "added")); 

Full code:

import 'dart:async';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:getx_shishi/app_config.dart';
import 'package:getx_shishi/tabs/add_index.dart';
import 'package:getx_shishi/tabs/home_index.dart';
import 'package:getx_shishi/tabs/my_index.dart';
import 'package:getx_shishi/tabs/project_index.dart';

class TabsPage extends StatefulWidget {
  const TabsPage({super. key});

  @override
  State<TabsPage> createState() => _TabsPageState();
}

class _TabsPageState extends State<TabsPage> {
  // StreamController controller = StreamController();
  StreamController<Map> controller = StreamController. broadcast();

  List<BottomNavigationBarItem> bottomList = [
    BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
    BottomNavigationBarItem(icon: Icon(Icons.home), label: "item"),
    BottomNavigationBarItem(icon: Icon(Icons. home), label: "My"),
  ];

  List pageList = [];

  // current subscript
  int currentIndex = 0;
  int index = 0;
  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    // controller. onCancel();
    controller.onCancel!();
  }

  @override
  Widget build(BuildContext context) {
    print("Come in and rebuild");
    print(bottomList. length);
    print("The length of the page----${pageList.length}");
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        type: BottomNavigationBarType. fixed,
        onTap: (value) {
          // setState(() {
          currentIndex = value;
          getData();
          // });
        },
        items: bottomList,
      ),
      body: IndexedStack(
        index: currentIndex,
        children: pageList.map<Widget>((e) => e["widget"]).toList(),
      ),
    );
  }

  getData() {
    if (index == 0) {
      pageList = [
        //When initializing
        {
          "have": false,
          "title": "Homepage",
          "widget": HomeIndexPage(
            streamController: controller, // Writing here is the same as defining and initializing at the beginning (there is a variable control)
          ),
        },
        {
          "have": false,
          "title": "Project",
          "widget": ProjectIndexPage(
            streamController: controller,
          ),
        },
        {
          "have": false,
          "title": "My",
          "widget": MyIndexPage(
            streamController: controller,
          ),
        },
      ];
    }
    if (index == 0) {
      AppConfig. originalIndex = true;
      AppConfig. currentIndex = true;
      // getContent();
    }
    if (index == 5) {
      AppConfig. originalIndex = false;
    }
    if (index == 10) {
      AppConfig. originalIndex = true;
    }
    //if()
    getContent();
    index + + ;

    controller.add({"title": pageList[currentIndex]["title"]}); // Click the bottom page to refresh
    setState(() {});
  }

  getContent() {
    if (index == 0 & amp; & amp;
        AppConfig.originalIndex == true & &
        AppConfig. currentIndex == true) {
      // This is what came in at the beginning

      bottomList.insert(
          1,
          BottomNavigationBarItem(
              icon: Icon(Icons.add), label: "Added")); // But it's right at the beginning, and it's wrong to change it halfway
      pageList.insert(
        1,
        {
          "have": true,
          "title": "Add",
          "widget": AddIndexPage(
            streamController: controller,
          ),
        },
      );
      setState(() {});
      controller.add({"title": pageList[currentIndex]["title"]});
      return;
    }

    if (index != 0 & amp; & amp; AppConfig. originalIndex != AppConfig. currentIndex) {
      if (AppConfig. originalIndex == false) {
        List list = pageList
            .where((element) => element["have"])
            .toList(); //Check if the added page is included
        if (list. isNotEmpty) {
          bottomList. removeAt(1);
          pageList. removeAt(1);
          if (currentIndex >= 1) {
            currentIndex = currentIndex - 1;
          }
        }
        AppConfig.currentIndex = AppConfig.originalIndex;

        return;
      }

      if (AppConfig. originalIndex = true) {
        List list = pageList
            .where((element) => element["have"])
            .toList(); //Check if the added page is included
        if (list. isEmpty) {
        // bottomList.insert(
          // 1, BottomNavigationBarItem(icon: Icon(Icons.add), label: "added"));
       //You can't write like this here, it will report an error, report a subscript error, and I don't know why
          // The following RangeError was thrown building BottomNavigationBar(dirty,
         // state:
          // _BottomNavigationBarState#a56dd(tickers: tracking 3 tickers)):
          // RangeError (index): Invalid value: Not in range 0..2, inclusive: 3
          bottomList = [
            //This way direct assignment is fine
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
            BottomNavigationBarItem(icon: Icon(Icons.add), label: "added"),
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "item"),
            BottomNavigationBarItem(icon: Icon(Icons. home), label: "My"),
          ];
          pageList.insert(
            1,
            {
              "have": true,
              "title": "Add",
              "widget": AddIndexPage(
                streamController: controller,
              ),
            },
          );
         
          AppConfig.currentIndex = AppConfig.originalIndex;
          setState(() {
            if (currentIndex >= 1) {
              currentIndex = currentIndex + 1;
            }
          });

         
        }

        return;
      }
     
    }
  }
}