The name 'Task' isn't a type so it can't be used as a type argument. | Flutter & Dart

Viewed 283

I just got an error. It says :

> The name 'Task' isn't a type so it can't be used as a type argument.
> 
> Try correcting the name to an existing type, or defining a type named
> 'Task'.

Here's the snippet code :

Expanded(
                child: FutureBuilder(
                  initialData: [],
                  future: _dbHelper.getTasks(),
                  builder: (context, AsyncSnapshot<List<Task>> snapshot) {
                    return ScrollConfiguration(
                      behavior: NoGlowBehaviour(),
                      child: ListView.builder(
                        itemCount: snapshot.data!.length,
                        itemBuilder: (context, index) {
                          return GestureDetector(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (context) => Taskpage(),
                                ),
                              );
                            },
                            child: TaskCardWidget(
                              title: snapshot.data![index].title,
                            ),
                          );
                        },
                      ),
                    );
                  },
                ),
              ),

Here's the snippet code Picture : Snippet Error Code Frans

Here's my full code: https://github.com/kisekifrans/todo_furans

If I imported the task library, more errors showed up.

Please help me out with this, and have a great day!

2 Answers

Task seems like a class defined in https://github.com/kisekifrans/todo_furans/blob/main/lib/models/task.dart. Of course you should import here to use it.

Importing Task causes other error, which is The argument type 'ScrollConfiguration Function(BuildContext, AsyncSnapshot<List<Task>>)' can't be assigned to the parameter type 'Widget Function(BuildContext, AsyncSnapshot<List<dynamic>>)', just change

builder: (context, AsyncSnapshot<List<Task>> snapshot) {

to

builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {

The code below should do the trick. Notice the commented lines where the changes are.

First, you need to import Task with:

import 'package:todo_furans/models/task.dart';

Second, you need to remove the AsyncSnapshot<List<Task>> type redefinition on the snapshot parameter. This is because snapshot has a type already that comes from the default FutureBuilder<Object?> and Dart errors out telling the function signature types are not compatible with AsyncSnapshot<List<dynamic>>. So, the easiest way is to declare FutureBuilder itself with the type you want snapshot to be. Like FutureBuilder<List<Task>>.

FutureBuilder<List<Task>>( // <- Here
  initialData: [],
  future: _dbHelper.getTasks(),
  builder: (context, snapshot) { // <- Here
    return ScrollConfiguration(
      behavior: NoGlowBehaviour(),
      child: ListView.builder(
        itemCount: snapshot.data!.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => Taskpage(
                    task: snapshot.data![index],
                  ),
                ),
              );
            },
            child: TaskCardWidget(
              title: snapshot.data![index].title,
            ),
          );
        },
      ),
    );
  },
),

Finally, I'd recommend checking if snapshot.data is null first instead of forcing it to non-null with ! operator. While it's null just show a CircularProgressIndicator to indicate it's still loading.

Related