【问题标题】:I think i have done something that's setstate is not working in flutter我想我做了一些 setstate 不能正常工作的事情
【发布时间】:2021-09-24 08:11:30
【问题描述】:

我对这个领域很陌生,所以请放轻松。我正在学习颤振我在我的应用程序中制作了一张卡片并给它一个删除按钮,但该按钮没有删除任何卡片。请帮帮我。 这是我的代码。

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'quotes.dart';
import 'quotes_list.dart';
void main() => runApp(new MaterialApp(
  home: Quotelist(),
));

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

  @override
  _QuotelistState createState() => _QuotelistState();
}

class _QuotelistState extends State<Quotelist> {

  List<quotes> q = [
    quotes(text: "once upon a time", author: "Shayan Ali"),
    quotes(text: "once upon a time", author: "Sumair Ali"),
    quotes(text: "once upon a time", author: "Umair Ali")
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.amber[200],
        appBar: AppBar(
        title: Text("list"),
        centerTitle: true,
        backgroundColor: Colors.amber[800],
       ),
      body: Column(
         children: q.map((e) => Quotecard(quote: e,
        delete: (){
          setState(() {
            q.remove(e);
          });
        },
        ),
        ).toList(),
      ),
    );
  }
}

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

class Quotecard extends StatelessWidget {

  final quotes? quote;
  final Function? delete;
  Quotecard({ this.quote, this.delete });

  @override
  Widget build(BuildContext context) {
     return Card(
      margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0),
      color: Colors.amber[900],
      child: Padding(
        padding: const EdgeInsets.all(12.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text(
              quote?.text ?? "",
              style: TextStyle(
              fontSize: 18,
               color: Colors.amber[200]
              ),
            ),
            SizedBox(height: 3,),
            Text(
              quote?.author ?? "",
              style: TextStyle(
                fontSize: 18,
                color: Colors.amber[200]
              ),
            ),
            SizedBox(height: 8,),
            TextButton.icon(onPressed: (){
              delete;
            }, 
             icon: Icon(Icons.delete), 
            label: Text("delete quote"))
          ],
        ),
      ),
    );
  }
}

class quotes {
  String? text;
  String? author;

  quotes({this.text,this.author});
}

我认为这种情况不能正常工作

body: Column(
         children: q.map((e) => Quotecard(quote: e,
        delete: (){
          setState(() {
            q.remove(e);
          });

请大家帮我解决这个问题,我尽了最大努力,但从 3 天开始仍然陷入这个问题。

【问题讨论】:

    标签: android list flutter button setstate


    【解决方案1】:

    我注意到代码中的 delete 缺少 () 的两件事,请改用 delete()

    TextButton.icon(onPressed: (){
                  delete;
                },
    

    setState(() {
                q.remove(e);
              });
    

    改用这个

    q.remove(e);
    setState(() {});
    

    在回调中

    【讨论】:

      【解决方案2】:

      TextButton.icononPressed 更改为:

      onPressed : () => delete(),
      

      delete 后面的“()”是执行函数所必需的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-24
        • 1970-01-01
        • 2011-04-12
        • 2011-06-24
        • 1970-01-01
        • 2014-10-07
        • 2011-02-27
        • 1970-01-01
        相关资源
        最近更新 更多