Home > Blockchain >  The argument type 'Future<List<Exercise>>' can't be assigned to the param
The argument type 'Future<List<Exercise>>' can't be assigned to the param

Time:09-17

I am trying to use data from my Cloud Firestore database, but I am getting this error which and I have no idea why I am getting this error or how to fix it. As far as I am concerned the two datatypes the error is on about are the same.

Here is the error: The argument type 'Future<List<Exercise>>' can't be assigned to the parameter type 'Future<List<Exercise>>?' Here is the code snippet the error is occurring at. The error is on this part: FirestoreService().getExercises()

child: FutureBuilder<List<Exercise>>(
   future: FirestoreService().getExercises(),
   builder: (context, snapshot)

Full code: exercise_list.dart

class ExerciseList extends StatelessWidget {
  const ExerciseList({super.key});

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: DecoratedBox(
        decoration: const BoxDecoration(
          color: Colors.white,
        ),
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 30),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: FutureBuilder<List<Exercise>>(
                  future: FirestoreService().getExercises(),
                  builder: (context, snapshot) {
                    if (snapshot.hasError) {
                      return const Text('Something went wrong');
                    }

                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return const Center(
                        child: CircularProgressIndicator(),
                      );
                    }

                    return ListView.builder(
                      itemCount: snapshot.data!.length,
                      itemBuilder: (context, index) => ExerciseItem(
                        exercise: snapshot.data![index],
                      ),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

firestore.dart

/// Reads all documents from the exercises collection
  Future<List<Exercise>> getExercises() async {
    var ref = _db.collection('exercises');
    var snapshot = await ref.get();
    var data = snapshot.docs.map((s) => s.data());
    var exercises = data.map((d) => Exercise.fromJson(d));
    return exercises.toList();
  }

Any help would would be much appreciated

CodePudding user response:

I did not test this, but I think the error occurs because Future<List<Exercise>> is not the same as Future<List<Exercise>>?.

So, getExercises() should return Future<List<Exercise>>? instead of Future<List<Exercise>>.

CodePudding user response:

FutureBuilder by default return a nullable value, but your method that pass to FutureBuilder is returning non-nullable variable, so change your getExercises() to this:

Future<List<Exercise>>? getExercises() async {
    var ref = _db.collection('exercises');
    var snapshot = await ref.get();
    var data = snapshot.docs.map((s) => s.data());
    var exercises = data.map((d) => Exercise.fromJson(d));
    return exercises.toList();
  }
  • Related