Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error calling 'Void' on 'onPressed/onTap' Flutter

Tags:

void

flutter

dart

I get an error calling a void in onPressed/onTap onPressed no details onTap with detail

I'm trying to call a set state in an Inherited widget/state following this instructions https://medium.com/flutter-community/widget-state-buildcontext-inheritedwidget-898d671b7956

To my inexpert eyes looks like he is doing the same, but clearly I missed something; can you help me out?

   class InhCore extends InheritedWidget {
  InhCore({Key key, @required Widget child, @required this.data})
      : super(key: key, child: child);

  final InhState data;

  @override
  bool updateShouldNotify(InhCore oldWidget) {
    return true;
  }
}

class InhWidget extends StatefulWidget {
  InhWidget({this.child});

  final Widget child;

  @override
  State<StatefulWidget> createState() => InhState();

  static InhState of(BuildContext context) {
    return (context.inheritFromWidgetOfExactType(InhCore) as InhCore).data;
  }
}

class InhState extends State<InhWidget> {
  final Map<String, int> cardMap = {
    'A':1,
    '2':2,
    '3':3,
    '4':4,
    '5':5,
    '6':6,
    '7':7,
    '8':8,
    '9':9,
    '10':0,
    'J':0,
    'Q':0,
    'K':0
  };

  List<String> cardDisplayed;

  void deal(String card) => setState(() => cardDisplayed.add(card));


  @override
  Widget build(BuildContext context) {
    return new InhCore(
      data: this,
      child: widget.child,
    );
  }
}

class CardButton extends StatelessWidget {
  final String input;
  CardButton({this.input});

  Widget build(BuildContext context) {
    final InhState state = InhWidget.of(context);
    return Container(
      width: 55.0,
      height: 55.0,
      child: Material(
        color: Colors.grey[200],
        child: InkWell(
          onTap: state.deal(input),
          child: Center (
            child:Text(input),
        ),
        ),
      ),
    );
  }
}

thanks in advance for the help

like image 562
Francesco Iapicca Avatar asked Nov 15 '18 15:11

Francesco Iapicca


1 Answers

You missed () =>

onPressed: () => state.deal(input)

Your code passes the result of the call state.deal(input) to onPressed, while above code passes a function that when called invokes state.deal(input)

like image 194
Günter Zöchbauer Avatar answered Nov 14 '22 13:11

Günter Zöchbauer