I want to access one or more lists or variables throughout the tree of my program and also perform operations of adding, deleting, editing on that list, I solve this problem by using the provider package as below figure:
I in first make this class
class TaskData extends ChangeNotifier {
List<Task> _tasks = [
Task(title: 'Buy Milk'),
Task(title: 'Buy Eggs'),
Task(title: 'Buy Bread'),
];
UnmodifiableListView<Task> get tasks => UnmodifiableListView(_tasks);
int get taskCount => _tasks.length;
toggleDone(index) {
_tasks[index].toggleDone();
notifyListeners();
}
addTask(data) {
_tasks.add(Task(title: data));
}
removeTask(index) {
_tasks.removeAt(index);
notifyListeners();
}
}
Then provide tree like this
void main() {
runApp(ChangeNotifierProvider(
create: (BuildContext context) => TaskData(),
child: MaterialApp(
theme: ThemeData(
useMaterial3: true,
),
home: TasksScreen(),
),
));
}
And i use like this code
class TasksList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<TaskData>(
builder: (context, taskData, child) {
return ListView.builder(
itemCount: taskData.taskCount,
itemBuilder: (context, index) {
print(taskData.tasks[index]);
return TaskTile(
taskTitle: taskData.tasks[index].title,
isChecked: taskData.tasks[index].isDone,
checkboxCallback: (newValue) {
taskData.toggleDone(index);
},
listTileCallback: () {
taskData.removeTask(index);
},
);
},
);
},
);
}
}
Now, I need to learn how to do this with the bloc and flutter_bloc and clean architecture that such as model class and entity class and repository class and all clean architecture system?