参数类型“Future<List<Exercise>>”无法分配给参数类型“Future<List<Exercise>>?”

问题描述 投票:0回答:3

我正在尝试使用 Cloud Firestore 数据库中的数据,但收到此错误,并且我不知道为什么收到此错误或如何修复它。就我而言,错误所在的两种数据类型是相同的。

错误如下:

The argument type 'Future<List<Exercise>>' can't be assigned to the parameter type 'Future<List<Exercise>>?'
这是发生错误的代码片段。错误就在这部分:
FirestoreService().getExercises()

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

完整代码: 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();
  }

任何帮助将不胜感激

flutter firebase dart google-cloud-firestore
3个回答
1
投票

FutureBuilder
默认返回一个可为空的值,但是传递给
FutureBuilder
的方法返回不可为空的变量,因此将
getExercises()
更改为:

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();
  }

0
投票

我没有测试这个,但我认为发生错误是因为

Future<List<Exercise>>
Future<List<Exercise>>?
不一样。

因此,

getExercises()
应该返回
Future<List<Exercise>>?
而不是
Future<List<Exercise>>


0
投票

遇到了同样的问题...这是一个奇怪的错误,但一个简单的解决方案是像 FutureBuilder 中那样转换函数返回值:

FutureBuilder<List<Exercise>>(
   future: FirestoreService().getExercises() as Future<List<Exercise>>,
   builder: (context, snapshot) { ... }
© www.soinside.com 2019 - 2024. All rights reserved.