I'm trying to initialize my data model object in my parent widget but i'm getting the following error "Only static members can be accessed in initializers". I'm trying to figure out how to get around this issue. I also want to to know if this is the proper way of initializing my data model in my parent widget or if there is a better way of doing this.
I have a simple TodoList model that is responsible for handling all the logic for managing a users todo.
class TodoList {
List<Todo> _items = [
Todo(
id: '1',
task: 'do laundry',
note: 'don\'t forget to seperate the whites')
];
List<Todo> get items {
return [..._items];
}
void toggleAll() {}
bool _hasIncompletedTodos() {
return _items.any((todo) => !todo.complete);
}
}
I'm not using any state management libraries and am just going to store all my state in the root Widget(HomeScreen) and pass all the state down to child widgets.
class _HomeScreenState extends State<HomeScreen> {
TodoList todoList;
int _selectedPageIndex = 0;
List<Map<String, dynamic>> _pages = [
{
'page': TodosListScreen(
todos: todoList.items, <<< Only static members can be accessed in initializers
),
'title': 'Todos'
},
{'page': TodosStatsScreen(), 'title': 'Stats'}
];
I saw this post and it recommended to move everything into initState. I tried doing that but i still get the same error
Only static members can be accessed in initializer. Dart2.0
class _HomeScreenState extends State<HomeScreen> {
TodoList todoList;
int _selectedPageIndex = 0;
@override
void initState() {
super.initState();
todoList = TodoList();
}
List<Map<String, dynamic>> _pages = [
{
'page': TodosListScreen(
todos: todoList.items, <<<< same error
),
'title': 'Todos'
},
{'page': TodosStatsScreen(), 'title': 'Stats'}
];