Flutter:如何在调用阻塞之前使用用户确认对话框

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

我试图在调用blocprovider之前调用configramtion dailog,但我得到了这个错误:

BlocProvider.of() called with a context that does not contain a DatasendBloc.
        No ancestor could be found starting from the context that was passed to BlocProvider.of<DatasendBloc>().

        This can happen if the context you used comes from a widget above the BlocProvider.

        The context used was: Builder

这是我的代码,我把它放在函数中,并从listview.bulder调用它:

void _showConfirmationDialog(BuildContext context, postmodel model) {
  showDialog(
    context: context,
    builder: (BuildContext dialogContext) {
      return AlertDialog(
        title: Text('Confirm Action'),
        content: Text('Do you want to perform this action?'),
        actions: <Widget>[
          TextButton(
            child: Text('Cancel'),
            onPressed: () {
              Navigator.pop(dialogContext); // Close the dialog
            },
          ),
          TextButton(
            child: Text('OK'),
            onPressed: () async {
              // Call the bloc here
              try {
                BlocProvider.of<DatasendBloc>(dialogContext).add(SendData(model));
              } catch (e) {
                print(e);
                await NotificationService.showNotification(
                  title: "Fail",
                  body: "Error occurred, please try again",
                );
              }
              Navigator.pop(dialogContext); // Close the dialog
            },
          ),
        ],
      );
    },
  );

错误来自BlocProvider.of(dialogContext).add(SendData(model));.如何解决这个问题?
我试过调用不同的构建上下文。在调用这个页面之前,我调用blocprovider,如下所示:

child: ElevatedButton(
                  onPressed: () {
                        Navigator.push(
                      context,
                      MaterialPageRoute(
                    builder: (_) => BlocProvider<DatasendBloc>.value(
                      value: _PostingBloc,
                      child: const SaveformApp(),
                    ),
                  )
                    );
                      },
                      
                  child: const Text('List of IDs Inspected'),
                )

如何克服这一点?

pftdvrlh

pftdvrlh1#

发生错误的原因是您在另一个上下文中提供并创建了Bloc。
您可以将上下文视为不同的层,每个层都需要在其中创建和提供Bloc。但请注意,在其上下文中提供和创建的每个Bloc都是独立的。因此,如果您在Dialog中提供并创建另一个Bloc,则无法使用generalContext Bloc的State
有几种方法可以解决你的问题:

  • 我推荐的方法是在Navigator.pop()函数中添加第二个参数。你可以这样做
showDialog(
    context: context,
    builder: (BuildContext dialogContext) {
      return AlertDialog(
        actions: <Widget>[
          TextButton(
            onPressed: () {
              Navigator.pop(dialogContext, true);
            },
          ),
          TextButton(
            onPressed: () async {
              Navigator.pop(dialogContext, false);
            },
          ),
        ],
     ).then((value) {
    //Call your bloc from general context here
    if(value) {
    //Do your thing if user says no
    } else{
    //Do your thing if user says no
    }
  });

我认为这是一个很好的实践,因为它将逻辑层从UI层中分离出来,并且Bloc尽可能保持独立

相关问题