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

Time:06-26

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!

CodePudding user response:

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) {

CodePudding user response:

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 and dart errors out telling the function signature types are not compatible with AsyncSnapshot<List<dynamic>>. Then, you should cast the snapshot.data to the desired type, i.e. The return type of _dbHelper.getTasks(). I've created a variable list for that.

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

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