flutter dart返回值中的Future函数

bis0qfac  于 2023-10-22  发布在  Flutter
关注(0)|答案(1)|浏览(180)

我现在要做的是根据boolean的值将升高的按钮显示为红色或蓝色。
布尔值将从函数getbool获取其值。getboolFirebase中的数组与给定的字符串进行比较,如果存在,则返回true,否则返回false。我想将返回值赋给一个boolean变量,这样我就可以将提升的按钮构建为红色或蓝色。但它抛出了这个错误:

A value of type 'Future<bool>' can't be assigned to a variable of type 'bool'.
Try changing the type of the variable, or casting the right-hand type to 'bool'

我知道状态管理或FutureBuilder可以让它变得容易,但我想知道为什么我的代码不能正常工作。我在getbool方法中使用了bloc和await,我也不能让构建方法bloc来解决这个问题。所以我在尝试做一些不可能的事吗想了解一下这个。

class CoursePage extends StatelessWidget {
  late String courseName;
  late Future<CourseModel> course = CourseModel.fromSnap(courseName);
  late Future<UserModel> profile = UserModel.fromId(AuthMethods().uid);
  //bool joinorExit = getbool(courseName, AuthMethods().uid);
  CoursePage({super.key, required this.courseName});
  late bool joinorExit;
  @override
  Widget build(BuildContext context) {
  joinorExit = getbool(courseName, AuthMethods().uid);

    return Scaffold(
        backgroundColor: Colors.teal,
        body: FutureBuilder(
            future: course,
            builder: (context, AsyncSnapshot<CourseModel> snapshot) {
              if (snapshot.hasData) {
                return Column(
                  children: [
                    Text(snapshot.data!.Department),
                    joinorExit
                        ? ElevatedButton(
                            style: ElevatedButton.styleFrom(
                                backgroundColor: Colors.red),
                            onPressed: () => StudyGroupModel.CreateGroup(
                                courseName, AuthMethods().uid),
                            child: Text('join'))
                        : ElevatedButton(
                            style: ElevatedButton.styleFrom(
                                backgroundColor: Colors.blue),
                            onPressed: () => StudyGroupModel.CreateGroup(
                                courseName, AuthMethods().uid),
                            child: Text('join')),
                    Text(snapshot.data!.Number),
                    Text(snapshot.data!.Name),
                    Text(snapshot.data!.Description),
                  ],
                );
              } else if (snapshot.hasError) {
                return const AlertDialog(title: Text("Error"));
              } else {
                return const CircularProgressIndicator();
              }
            }));
  }
}

Future<bool> getbool(String course, String id) async {
  bool joinExit = false;

  FirestoreService temp = FirestoreService.instance;
  if (await temp.documentExists(path: FirestorePath.studyGroupsspe(course))) {
    await temp.documentFuture(
        path: FirestorePath.studyGroupsspe(course),
        builder: (data) {
          if (data['members'].contains(id)) {
            joinExit = true;
            //setbool(joinExit);
          } else {
            joinExit = false;
            // setbool(joinExit);
          }
        });
  }
  return joinExit;
}
dddzy1tm

dddzy1tm1#

发生错误是因为您试图将Future<bool>直接分配给bool变量。您可以使用另一个FutureBuilder来处理异步操作,并根据getbool方法的结果更新UI。

class CoursePage extends StatelessWidget {
  late String courseName;
  late Future<CourseModel> course = CourseModel.fromSnap(courseName);
  late Future<UserModel> profile = UserModel.fromId(AuthMethods().uid);

  CoursePage({super.key, required this.courseName});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.teal,
      body: FutureBuilder(
        future: course,
        builder: (context, AsyncSnapshot<CourseModel> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const CircularProgressIndicator();
          } else if (snapshot.hasError) {
            return const AlertDialog(title: Text("Error"));
          } else if (snapshot.hasData) {
            return FutureBuilder(
              future: getbool(courseName, AuthMethods().uid),
              builder: (context, AsyncSnapshot<bool> joinSnapshot) {
                if (joinSnapshot.connectionState == ConnectionState.waiting) {
                  return const CircularProgressIndicator();
                } else if (joinSnapshot.hasData) {
                  return Column(
                    children: [
                      Text(snapshot.data!.Department),
                      joinSnapshot.data!
                          ? ElevatedButton(
                              style: ElevatedButton.styleFrom(
                                  backgroundColor: Colors.red),
                              onPressed: () => StudyGroupModel.CreateGroup(
                                  courseName, AuthMethods().uid),
                              child: Text('join'))
                          : ElevatedButton(
                              style: ElevatedButton.styleFrom(
                                  backgroundColor: Colors.blue),
                              onPressed: () => StudyGroupModel.CreateGroup(
                                  courseName, AuthMethods().uid),
                              child: Text('join')),
                      Text(snapshot.data!.Number),
                      Text(snapshot.data!.Name),
                      Text(snapshot.data!.Description),
                    ],
                  );
                } else {
                  return const CircularProgressIndicator();
                }
              },
            );
          } else {
            return const CircularProgressIndicator();
          }
        },
      ),
    );
  }
}

相关问题