skip to Main Content

I’m using an AppValueNotifier to create a value called showStreamDialogNotifier that will change depending on some stream. When this value becomes true, I want to show a dialog box on my Login form.

class AppValueNotifier{
  ValueNotifier showStreamDialogNotifier = ValueNotifier<bool>(false);

  void updateShowStreamDialogNotifier({required bool? newShowStreamDialog, required String? newStreamDialogTitle, required String? newStreamDialogMsg}) {
    if (newShowStreamDialog != null) {
      showStreamDialogNotifier = ValueNotifier<bool>(newShowStreamDialog);
    }

  }

}

The problem is that when I try to call my function that runs showDialog, it gives the error in the title. Why is this error appearing and how do I modify my code so that it properly shows a dialog over the Login form when showStreamDialogNotifier = true?

class Login extends StatefulWidget {
  const Login({super.key});

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {

    Future<void> showStreamDialog({required BuildContext context, required String title, required String msg}) {
        return showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text(title),
                content: SingleChildScrollView(
                  child: ListBody(
                    children: <Widget>[
                      Text(msg),
                    ],
                  ),
                ),
                actions: <Widget>[
                  TextButton(
                    child: const Text('OK'),
                    onPressed: () {
                      final globalsOneTimeRead = context.read<GlobalsProvider>();
                      globalsOneTimeRead.updateStreamDialogVariables(newShowStreamDialog: false, newStreamDialogTitle: '', newStreamDialogMsg: '');
                      context.pop();
                    },
                  ),
                ],
              );
            }
        );
      }

    @override
      Widget build(BuildContext context) {
        AppValueNotifier appValueNotifier = AppValueNotifier();
        
        return Consumer<GlobalsProvider>(
            builder: (context, globals, child) => Scaffold(
                body: Center(
                    child: Column(
                        children: [
                            Expanded( 
                                child: ValueListenableBuilder( //THE ERROR POINTS TO THIS LINE
                                    valueListenable: appValueNotifier.showStreamDialogNotifier,
                                    builder: (context, value, child) {

                                      if (value == true) {
                                        showStreamDialog(context: context, title: globals.streamDialogTitle, msg: globals.streamDialogMsg);
                                      }
                                      
                                      return Text('');
                                    },
                                ),
                            ),
                        ]
                    )
                )
            )

        

    }

}

2

Answers


  1. While you would like to show dialog, I will prefer adding listener instead.

    AppValueNotifier appValueNotifier = AppValueNotifier();
    
      @override
      void initState() {
        super.initState();
        appValueNotifier.showStreamDialogNotifier.addListener(() {
          if (appValueNotifier.showStreamDialogNotifier.value == true) {
             showStreamDialog(... );
          }
        });
      }
    
      @override
      void dispose() {
        appValueNotifier.showStreamDialogNotifier.dispose();
        super.dispose();
      }
    

    Also, you can try with postFrameCallback.

    Login or Signup to reply.
  2. WidgetsBinding.instance.addPostFrameCallback((_){
    
      // Add Your If Condition Code here
    
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search