Randu Pratama
Randu Pratama

Reputation: 23

How to Fix Error when using Provider Package Flutter

I'm creating an app like a note that contains a list of activities that you can add. I use the provider package to save the changes to the list. The list is created by ListView.builder, and the child is a ListTile consisting of text and a checkbox. there is no error in my coding, but in debugging there is an error "if you want to expose a variable that can be anything, consider to changing dynamic to object instead" like the image I gave. how can i fix it?

Main.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todoey_flutter/models/task_data.dart';
import 'screens/tasks_screen.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => TaskData(),
      lazy: false,
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        home: TasksScreen(),
      ),
    );
  }
}

task_list.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'task_tile.dart';

class TaskList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer(
      builder: (context, taskDataObject, child) {
        return ListView.builder(
          itemBuilder: (context, index) {
            return TaskTile(
              theTask: taskDataObject.tasks[index].name,
              isChecked: taskDataObject.tasks[index].isDone,
              checkboxCallback: (checkboxState) {
                taskDataObject.updateTask(taskDataObject.tasks[index]);
              },
            );
          },
          itemCount: taskDataObject.taskCount,
        );
      },
    );
  }
}

task_tile.dart

import 'package:flutter/material.dart';

class TaskTile extends StatelessWidget {
  TaskTile({
    this.isChecked,
    this.theTask,
    this.checkboxCallback,
    this.onLongPressCallback,
  });

  final bool isChecked;
  final String theTask;
  final Function checkboxCallback;
  final Function onLongPressCallback;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      onLongPress: onLongPressCallback,
      title: Text(
        theTask,
        style: TextStyle(
            decoration: isChecked ? TextDecoration.lineThrough : null),
      ),
      trailing: Checkbox(
        activeColor: Colors.lightBlueAccent,
        value: isChecked,
        onChanged: checkboxCallback,
      ),
    );
  }
}

the Provider class

import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:todoey_flutter/models/task.dart';

class TaskData extends ChangeNotifier {
  List<Task> _tasks = [
    Task(name: 'cook'),
    Task(name: 'reading'),
    Task(name: 'sleding')
  ];

  int get taskCount {
    return _tasks.length;
  }

  UnmodifiableListView<Task> get tasks {
    return UnmodifiableListView(_tasks);
  }

  void addTask (String newTaskTitle) {
    final task = Task(name: newTaskTitle);
    _tasks.add(task);
    notifyListeners();
  }

  void updateTask (Task task){
    task.toggleDone();
    notifyListeners();
  }

  void deleteTask (Task task){
    _tasks.remove(task);
    notifyListeners();
  }
}

The Error Message

Upvotes: 0

Views: 3309

Answers (1)

joaomarcos96
joaomarcos96

Reputation: 910

You have to use Consumer of T, instead of only Consumer. So, change task_list.dart to be like this:

class TaskList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<TaskData>( // <--- Here, insert <TaskData> after Consumer
      builder: (context, taskDataObject, child) {
        // ...
      },
    );
  }
}

Upvotes: 2

Related Questions