Flutter: Why is my function passed to `StatelessWidget` not is not being executed?

prmethus

I was following a tutorial online. At a part, a function was passed to the QuoteCard class as an argument but when it was used inside the onPressed attribute Tenter code hereextButton widget, the function wasn't being executed. I tried using Function() and VoidCallback instead of Function, but it still didn't work.

import 'package:flutter/material.dart';
import 'quotes.dart';
import 'quotecard.dart';

void main() => runApp(MaterialApp(home: QuoteList()));

class QuoteList extends StatefulWidget {
  @override
  State<QuoteList> createState() => _QuoteListState();
}

class _QuoteListState extends State<QuoteList> {
  List<Quote> quotes = [
    Quote(
        author: 'Rudyard Kipling',
        text:
            "Down to Gehenna or up to the throne, he travels the fastest who travels alone."),
    Quote(author: 'Osca Wilde', text: "Ehlo"),
    Quote(author: 'Osca Wilde', text: "Ehlo"),
  ];

  Widget quoteTemplate(quote) {
    return new QuoteCard(
        quote: quote,
        delete: () {
          setState() {
            quotes.remove(quote);
            print("Delete Button Pressed");
          }
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[200],
        appBar: AppBar(
          title: Text("Awesome Quotes"),
          centerTitle: true,
          backgroundColor: Colors.redAccent,
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: quotes.map((quote) {
            return quoteTemplate(quote);
          }).toList(),
        ));
  }
}

class QuoteCard extends StatelessWidget {
  final Quote quote;
  final VoidCallback delete;
  QuoteCard({required this.quote, required this.delete});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(20.0, 10, 10.0, 0),
      child: Card(
          margin: EdgeInsets.fromLTRB(16, 16, 16, 0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Text(quote.text,
                  style: TextStyle(fontSize: 18.0, color: Colors.grey[600])),
              SizedBox(height: 6.0),
              Text(
                quote.author,
                style: TextStyle(fontSize: 14, color: Colors.grey[800]),
              ),
              SizedBox(height: 10.0),
              TextButton(
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[Text("Delete"), Icon(Icons.delete)]),
                onPressed: delete,
              )
            ],
          )),
    );
  }
}

class Quote {
  String text;
  String author;

  Quote({required this.text, required this.author});
}

What's the problem here?

MendelG

In your code when you do:

Widget quoteTemplate(quote) {
    return new QuoteCard(
        quote: quote,
        delete: () {
          setState() {
            quotes.remove(quote);
            print("Delete Button Pressed");
          }
        });
  }

setState accepts a void Function(void Function()), however, your not providing that, your missing the ().

Replace:

Widget quoteTemplate(quote) {
    return new QuoteCard(
        quote: quote,
        delete: () {
          setState() {
            quotes.remove(quote);
            print("Delete Button Pressed");
          }
        });
  }

with:

Widget quoteTemplate(quote) {
    return new QuoteCard(
        quote: quote,
        delete: () {
          setState(() {
            quotes.remove(quote);
          });
        });
  }

Complete runnable example using your code:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: QuoteList()));

class QuoteList extends StatefulWidget {
  @override
  State<QuoteList> createState() => _QuoteListState();
}

class _QuoteListState extends State<QuoteList> {
  List<Quote> quotes = [
    Quote(
        author: 'Rudyard Kipling',
        text:
            "Down to Gehenna or up to the throne, he travels the fastest who travels alone."),
    Quote(author: 'Osca Wilde', text: "Ehlo"),
    Quote(author: 'Osca Wilde', text: "Ehlo"),
  ];

  Widget quoteTemplate(quote) {
    return new QuoteCard(
        quote: quote,
        delete: () {
          setState(() {
            quotes.remove(quote);
          });
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[200],
        appBar: AppBar(
          title: Text("Awesome Quotes"),
          centerTitle: true,
          backgroundColor: Colors.redAccent,
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: quotes.map((quote) {
            return quoteTemplate(quote);
          }).toList(),
        ));
  }
}

class QuoteCard extends StatelessWidget {
  final Quote quote;
  final VoidCallback delete;
  QuoteCard({required this.quote, required this.delete});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(20.0, 10, 10.0, 0),
      child: Card(
          margin: EdgeInsets.fromLTRB(16, 16, 16, 0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Text(quote.text,
                  style: TextStyle(fontSize: 18.0, color: Colors.grey[600])),
              SizedBox(height: 6.0),
              Text(
                quote.author,
                style: TextStyle(fontSize: 14, color: Colors.grey[800]),
              ),
              SizedBox(height: 10.0),
              TextButton(
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[Text("Delete"), Icon(Icons.delete)]),
                onPressed: delete,
              )
            ],
          )),
    );
  }
}

class Quote {
  String text;
  String author;

  Quote({required this.text, required this.author});
}


Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Flutter: Why is my custom function passed to a `StatelessWidget` object not being executed?

Why my function in C++ is not being executed?

Why is my search function not being passed?

Why is nothing being passed to my overloaded function?

Why is a function being passed in?

Why aren't the values from my directive being passed into the function?

Why my last divison is not being executed?

Why is my program called "set" not being executed?

Why is my Print statement not being executed?

Why is my python class being executed twice?

Why is my ~/.profile being executed on opening a terminal?

This is the case for column in my StatelessWidget in Flutter:

Why is Async function not being executed in python

Javascript function not being executed. Why?

Parameters of my function in React child component are not being passed to the parent component's function. Why is this happening?

Why are my meteor settings not being passed to the application?

Why is my array being passed with an incorrect size?

Why is Context not being passed to my HOC

Why is my input not being passed into the if statements?

From function Widget to StatelessWidget in Flutter

Flutter :how to call function in statelesswidget?

My onChange function is not being passed down to child

Why isn't the return function inside my recursive call being executed?

How can I select the currently clicked div? and why is my function not being executed?

Why are my records not being inserted in the same order they are being executed?

Why is my parameter not passed into the function?

Why don't I have to specify that the result of a fortran function is being passed by value to my C++ program?

My React function being passed down as props is not being invoked

Why is my submit form not being executed in my Meteor app?