Can you get value added by flutter provider without a database?

Viewed 58

I am new to mobile development and I'm currently learning flutter by doing a simple to do app that can add, update and delete tasks and projects by using a simple app state management using provider package. Right now, I am having difficulties to show the added tasks in their selected projects. Basically, tasks in Project A is shared with tasks in Project B. Currently, I don't have any idea on how to make it work as I wanted it to without using a database as right now I am only learning basic CRUD by using provider package. I am wondering are there any way to make the tasks that is added in Project A to only show in Project A without it being shown in Project B also.

This is the Project provider

Class ProjectProvider extends ChangeNotifier {
  List<Project> _project = [
    Project(
      createdTime: DateTime.now(),
      title: 'Project 1',
      id: '1',
      left: 1,
      done: 1,
    ),
  ];

  List<Project> get project => _project;

  void generateProject(Project project) {
    _project.add(project);
    notifyListeners();
  }
}

This is the Project Task provider

class ProjectTaskProvider extends ChangeNotifier {
  List<ProjectTask> _projectTask = [
    ProjectTask(
      createdTime: DateTime.now(),
      title: 'Project Task 1',
      id: '1',
    ),
    ProjectTask(
      createdTime: DateTime.now(),
      title: 'Project Task 2',
      id: '2',
      description: 'Project Task 2 Description',
    ),
  ];
  List<ProjectTask> get projectTask =>
      _projectTask.where((projectTask) => projectTask.isDone == false).toList();

  List<ProjectTask> get ProjectTaskCompleted =>
      _projectTask.where((projectTask) => projectTask.isDone == true).toList();

  void addProjectTask(ProjectTask projectTask) {
    _projectTask.add(projectTask);
    notifyListeners();
  }

  void removeProjectTask(ProjectTask projectTask) {
    _projectTask.remove(projectTask);
    notifyListeners();
  }

  bool toggleProjectTask(ProjectTask projectTask) {
    projectTask.isDone = !projectTask.isDone;
    return projectTask.isDone;
  }

  void updateProjectTask(
      ProjectTask projectTask, String title, String description) {
    projectTask.title = title;
    projectTask.description = description;

    notifyListeners();
  }
}

Project Model

class ProjectField {
  static const createdTime = 'createdTime';
}

class Project {
  String id;
  DateTime createdTime;
  String? title;
  num? left;
  num? done;

  Project({
    required this.id,
    required this.createdTime,
    this.title,
    this.done,
    this.left,
  });
}

Project Task Model

class ProjectTaskField {
  static const createdTime = 'createdTime';
}

class ProjectTask {
  DateTime createdTime;
  String title;
  String id;
  String description;
  bool isDone;

  ProjectTask({
    required this.createdTime,
    required this.title,
    this.description = '',
    required this.id,
    this.isDone = false,
  });
}

This is the route that navigates from the project to project details

...
return GestureDetector(
      onTap: () {
        Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => ProjectDetails()));
      },
...
}

Add Project Page

Add Task Page

1 Answers

First of all if you just started learning, that's a very dope UI you pulled off great job. About you problem, you can easily achieve what you want without any database.

Just add a variable say "tag" for every task when the task is created on "Task page", which stores to which project the task belongs. So if a task is created from Project 1, the tag of task is equal to "Project 1".

Now when you display the saved task in "Project page" just apply a if conditional statement which only displays the task which are of that particular project and avoids the rest of them.

Hope it helps.

Related