Home > Back-end >  Flutter: Why is my function passed to `StatelessWidget` not is not being executed?
Flutter: Why is my function passed to `StatelessWidget` not is not being executed?

Time:07-05

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?

CodePudding user response:

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});
}


  • Related