skip to Main Content

I’m currently developing a todo app as taught in this tutorial on Youtube
https://www.youtube.com/watch?v=TclK5gNM_PM

I’m trying to practice fundamentals and decided to make a todo app for an entire week with a Drawer. for that I made some changed in the hive database and created 7 different pages with routes in main.dart.

I have converted the todoList from a list to a map of lists with 7 different lists associated with the respective days in the week.

Here is my code for database.dart after the changes.

import 'package:hive_flutter/hive_flutter.dart';

class ToDoData {
  final _myBox = Hive.box('myBox');

  Map<String, List> toDoList = {
    "monday": [],
    "tuesday": [],
    "wednesday": [],
    "thursday": [],
    "friday": [],
    "saturday": [],
    "sunday": [],
  };

  void createInitialData(){
    toDoList = {
      "monday": [["Create a new task.", false]],
      "tuesday": [["Create a new task.", false]],
      "wednesday": [["Create a new task.", false]],
      "thursday": [["Create a new task.", false]],
      "friday": [["Create a new task.", false]],
      "saturday": [["Create a new task.", false]],
      "sunday": [["Create a new task.", false]],
    };
  }

  void loadData(){
    var loadedData = _myBox.get("TODOLIST");
    if(loadedData != null && loadedData is Map){
      toDoList = Map<String, List>.from(
        loadedData.map((key, value) => MapEntry(
          key as String,
          List<dynamic>.from(value).map((e) => e as List).toList(),
        )),
      );
    }
    else {
      createInitialData();
    }
  }

  void updateData(){
    _myBox.put("TODOLIST", toDoList);
  }
}

After this, I have the following code to call the toDoTile from all different pages.dart

body: ListView.builder(
    itemCount: db.toDoList.length,
    itemBuilder: (context, index) {
        return ToDoTile(
            taskname: db.toDoList[day]![index][0],
            taskStatus: db.toDoList[day]![index][1], 
            onChanged: (value) => checkBoxChanged(value, index),
            deleteFunction: (context) => deleteTask(index),
        );
    },
),

I’m getting exception in the line after navigation. What I mean is that the app starts properly but when I navigate around using the drawer, this exception occurs.

taskname: db.toDoList[day]![index][0];

This is what the exception says

RangeError (RangeError (index): Invalid value: Only valid value is 0: 1)

I’m a beginner and I’ve no idea what is happening. Please help if possible.

2

Answers


  1. Your Map<String,list> was initial

     monday": [["Create a new task.", false]],
    

    So that mean your todoList[‘monday’] length gonna be 1 (index 0)

    and in your listview it gonna build 7 times because your item count is todoList.length (7)
    Idk what you want to do with your listview but if you want to change a day you can try this

    body: ListView.builder(
        itemCount: db.toDoList.length,
        itemBuilder: (context, index) {
            return ToDoTile(
                taskname: db.toDoList[toDoList.keys.toList()[index]]?[0][0] ?? '',
                taskStatus: db.toDoList[toDoList.keys.toList()[index]]?[0][1]?? '',
                onChanged: (value) => checkBoxChanged(value, index),
                deleteFunction: (context) => deleteTask(index),
            );
        },
    ),
    

    ps. this code will show only single task per day you need to imporve that

    Login or Signup to reply.
  2. body: ListView.builder(
      itemCount: db.toDoList[day]!.length,
      itemBuilder: (context, index) {
        return ToDoTile(
          taskname: db.toDoList[day]![index][0],
          taskStatus: db.toDoList[day]![index][1], 
          onChanged: (value) => checkBoxChanged(value, index),
          deleteFunction: (context) => deleteTask(index),
        );
      },
    ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search