Home > Net >  setState() or markNeedsBuild() called during build. This QuoteList widget cannot be marked as needin
setState() or markNeedsBuild() called during build. This QuoteList widget cannot be marked as needin

Time:07-20

i am learning flutter and i am taking demo in which a guy is calling a fucnction on button pressed. but i am getting an error : setState() or markNeedsBuild() called during build

please guide me what to do

below is my code

QuoteList.dart

import 'dart:ffi';
import 'quote.dart';
import 'package:flutter/material.dart';
import 'QuoteCard.dart';

void main() {
  runApp(MaterialApp(home: QuoteList()));
}

class QuoteList extends StatefulWidget {
  const QuoteList({Key? key}) : super(key: key);

  @override
  State<QuoteList> createState() => _QuoteListState();
}

class _QuoteListState extends State<QuoteList> {

  List<Quote> quotes = [
    Quote('Be yourself, Everyone else is already taken.','sana'),
    Quote('I have nothing to declare except my genius','sana'),
    Quote('The truth is rarely pure and never simple.', 'sana'),
  ];


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      appBar: AppBar(
        title: Text(
            "Awesome Quotes",
        ),
        centerTitle: true,
        backgroundColor: Colors.red,
      ),
      body: Column(
        children: quotes.map((quote) =>  QuoteCard(
          quote: quote,
          delete: () {
            setState(() {
            quotes.remove(quote);
            });
          }
        )).toList()

      )
    );
  }
}

and below is QuoteCard widget code. QuoteCard is a widget in seperate class and is called by main.dart file. The code is below for the quote card:

import 'dart:ffi';
import 'quote.dart';
import 'package:flutter/material.dart';

class QuoteCard extends StatelessWidget {

  final Quote quote;
  final Function delete;

  QuoteCard( {required this.quote, required this.delete });


  @override
  Widget build(BuildContext context) {
    return Card(

      margin: EdgeInsets.fromLTRB(16, 16, 16, 0),
      color: Colors.grey[100],
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Text(
                quote.text,
                style: TextStyle(
                  fontSize: 18,
                  color: Colors.grey[600],
                ),
              ),
              SizedBox(height: 6.0),
              Text (
                quote.author,
                style: TextStyle(
                  fontSize: 18,
                  color: Colors.grey[600],
                ),
              ),
              SizedBox(height: 6.0),
              FlatButton.icon(
                onPressed: delete(),
                icon: Icon(Icons.delete), 
                label: Text('delete')
                )

            ]
        ),
      ),
    );
  }
}

CodePudding user response:

It's coming from

onPressed: delete(),

which call the function every time the widget is built.

Try this instead

onPressed: delete,
  • Related