Home > other >  Flutter RangeError In Widget Array?
Flutter RangeError In Widget Array?

Time:07-21

I'm getting an error when I try to add widgets to a list in Dart. Does anyone know what this could mean? I've looked through the data, and through all of my code. Rewrote everything once or twice. I'm a tad rusty with Dart as I haven't used flutter in a while, any and all help is appreciated. TIA!

Code:

class _JobsListState extends State<JobsList> {
  @override
  Widget build(BuildContext context){

    String filename = widget.park.toLowerCase()   ".txt";
    String rawData = '';

    Future<String> parkData() async {
      return await rootBundle.loadString('assets/records/maps/'   filename);
    }

    void loadData() async {
      rawData = await parkData();
    }

    loadData();

    List <Widget> jobButtons = <Widget>[];
    List dataList = rawData.split(".");

    for (var job in dataList){

      debugPrint(job);

      List jobSplit = job.split("/");

      // bool def = (jobSplit[1] != 'R');
      bool def = false;
      String jobName = jobSplit[0].replaceAll('_', ' ');

      jobButtons.add(
          CheckboxListTile(
              value: def,
              title: Text(jobName),
              onChanged: (newValue){
                if (newValue == true){
                  rawData.replaceAll((jobSplit[0]   "/R"), (jobName   "/F"));
                } else {
                  rawData.replaceAll((jobSplit[0]   "/F"), (jobName   "/R"));
                }
              }
          )
      );
    }

    jobButtons.add(
      ElevatedButton(
          onPressed: (){
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => CutTrim(title: widget.park)),
            );
          },
          child: Text("Done"),
      )
    );

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: jobButtons
        )
      )
    );
  }
}

The error in question...

CodePudding user response:

You have used a stateful widget. The build method in stateful widget is called each time you set state. Because of that the list goes empty and resets to empty list. You have to initialise it in initstate.

  • Related