函数作为来自其他类函数的颤振中的参数

时间:2021-04-21 13:27:50

标签: flutter dart

我收到两个错误

  1. delete 未定义,但我在 quote_card.dart 文件中定义了删除,我将函数作为参数传递
    删除:(){setState(() {quotes.remove(quote);});

  2. delete 中没有找到的第二个参数是第二个参数但它没有考虑它
    QuoteCard(quote,delete:(){setState(() {quotes.remove(quote);});

主镖

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

void main() {
  runApp(MaterialApp(
    home: Quotes(),
  ));
}

class Quotes extends StatefulWidget {
  @override
  _QuotesState createState() => _QuotesState();
}

class _QuotesState extends State<Quotes> {
  List<Quote> quotes = [
    Quote(author: "shahab", text: 'he is here'),
    Quote(author: "shahab", text: 'he is here'),
    Quote(author: "shahab", text: 'he is here')
    
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.cyan[800],
      appBar: AppBar(
        title: Text('Listing the data'),
        centerTitle: true,
        backgroundColor: Colors.deepPurple[200],
      ),
      body: Column(
        children: quotes
            .map((quote) => QuoteCard(
                  quote,
                  // function for delete is not working but in the tutorial it was working
                  // https://www.youtube.com/watch?v=aqePcMyeoIY&t=184s  
                  delete:(){setState(() {
                    quotes.remove(quote);
                  });})).toList(),
      
    ))
    ;
  }
}

引用飞镖

class Quote {
  String text;
  String author;
  Quote({this.author, this.text});
}

quort_card

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

class QuoteCard extends StatelessWidget {
  final Quote quote;
  final Function delete;
  QuoteCard(this.quote, this.delete,);
  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.fromLTRB(15.0, 15.0, 15.0, 0),
      child: Padding(
        padding: EdgeInsets.all(6.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text(
              quote.text,
              style: TextStyle(
                fontSize: 18.0,
              ),
            ),
            SizedBox(
              height: 10.0,
            ),
            Text(
              quote.author,
              style: TextStyle(
                fontSize: 18.0,
              ),
            ),
            SizedBox(
              height: 10.0,
            ),
            // ignore: deprecated_member_use
            FlatButton.icon(
                onPressed: delete,
                icon: Icon(Icons.delete),
                label: Text('Delete'))
          ],
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:0)

您将 delete 定义为位置参数。将 Quotes 中的 QuoteCard 小部件更改为此

QuoteCard(quote, () {
                    setState(
                      () {
                        quotes.remove(quote);
                      },
                    );
                  })

答案 1 :(得分:0)

您只需在构造函数中添加 {} 即可使命名参数起作用。它应该看起来像这样,然后它也应该被找到:

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