简体   繁体   English

使用 flutter firebase 的 if 语句中未显示警报对话框

[英]Alert dialog not showing in an if statement using flutter firebase

I have a workout app inside it I have An alert Dialog widget, my idea is that if the user enters the app and he had not added an exercise before, I will show the alert dialog but, I wont let him dismiss the alert dialog using if statements and firebase, and if he had added an exercise before I don't wanna show the alert dialog.我里面有一个锻炼应用程序我有一个警报对话框小部件,我的想法是,如果用户进入应用程序并且他之前没有添加过锻炼,我将显示警报对话框但是,我不会让他使用关闭警报对话框if 语句和 firebase,如果他在我不想显示警报对话框之前添加了一个练习。

In this code the alert dialog is not appearing在此代码中,警报对话框未出现

code

//ExerciseStream class //练习流 class

late bool? firstLoad;

class ExerciseStream extends StatefulWidget {
  const ExerciseStream({Key? key}) : super(key: key);

  @override
  State<ExerciseStream> createState() => _ExerciseStreamState();
}

class _ExerciseStreamState extends State<ExerciseStream> {
  @override
  Widget build(BuildContext context) {
    CollectionReference users = FirebaseFirestore.instance.collection('users');
    return StreamBuilder<QuerySnapshot>(
        stream: users
            .doc(FirebaseAuth.instance.currentUser?.uid.toString())
            .collection('workout')
            .orderBy('exerciseName', descending: true)
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: CircularProgressIndicator());
          } else {
            if (snapshot.hasError) {
              return const Text('Error');
            } else {
              if (!snapshot.hasData) {
                firstLoad = false;
                Future.delayed(
                          Duration.zero,
                          () => CreateAlertDialog(
                              context, exerciseNameController, newWorkout))
                      .then((value) => firstLoad = true);
                print(firstLoad);
                return const Center(
                    child: Text('No Data',
                        style: TextStyle(color: backgroundColor)));
              } else {
                firstLoad = true;
                print(firstLoad);
                // code
              }
            }
          }
        });
  }
}

// CreateAlertDialog widget // CreateAlertDialog 小部件

Widget? CreateAlertDialog(
    BuildContext context, exerciseNameController, newWorkout) {
  showDialog(
      barrierDismissible: firstLoad ?? false,
      context: context,
      builder: (context) {
        return AlertDialog(
          // code
        );
      });
}

Thanks!!谢谢!!

Please follow flutter official demohttps://api.flutter.dev/flutter/material/AlertDialog-class.html请关注flutter官方demohttps://api.flutter.dev/flutter/material/AlertDialog-class.ZFC35FDC70D5FC69D7A33E2

your function should be like this.你的 function 应该是这样的。

       Future<void> _showMyDialog() async {
  return showDialog<void>(
    context: context,
    barrierDismissible: false, // user must tap button!
    builder: (BuildContext context) {
      return AlertDialog(
        title: const Text('AlertDialog Title'),
        content: SingleChildScrollView(
          child: ListBody(
            children: const <Widget>[
              Text('This is a demo alert dialog.'),
              Text('Would you like to approve of this message?'),
            ],
          ),
        ),
        actions: <Widget>[
          TextButton(
            child: const Text('Approve'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM