0

I create common alertbox and passed arguments. but when try tp pass onPressed method this error is coming.

Another exception was thrown: type 'bool' is not a subtype of type '() => void'

Widget class

return showDialogPop(
                      AppTranslations.of(context).text("login_error"),
                      AppTranslations.of(context).text("enter_password"),
                      AppTranslations.of(context).text("ok"),
                      Navigator.of(context).pop());

showDialogPop method

  showDialogPop(_titleText, _contentText, _childBtnText, VoidCallback _onPressed) {
    return showDialog(
        context: context,
        builder: (BuildContext context) => AppAlertDialog(
            titleText: _titleText,
            contentText: _contentText,
            childBtnText: _childBtnText,
            onPressed: _onPressed));
  }

common AppBar class

import 'package:flutter/material.dart';
class AppAlertDialog extends StatelessWidget {
  final VoidCallback onPressed;
  final String titleText;
  final String contentText;
  final String childBtnText;

  AppAlertDialog({
    this.titleText,
    this.contentText,
    this.childBtnText,
    this.onPressed,
  });

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(titleText),
      content: Text(contentText),
      actions: <Widget>[
        FlatButton(
            onPressed: () => onPressed(),
            child: Text(
              childBtnText,
              style: TextStyle(fontWeight: FontWeight.bold),
            ))],);}}
user9139407
  • 964
  • 1
  • 12
  • 25

1 Answers1

1

change Navigator.of(context).pop() to () => Navigator.of(context).pop()

i.e.

showDialogPop(
                      AppTranslations.of(context).text("login_error"),
                      AppTranslations.of(context).text("enter_password"),
                      AppTranslations.of(context).text("ok"),
                      () => Navigator.of(context).pop());
Tom Robinson
  • 531
  • 3
  • 7
  • 1
    Or into just `Navigator.of(context).pop`, creating a tear-off of the pop method. – lrn Jul 04 '19 at 05:33