异常:_TypeError 类型“Null”不是类型“(Quote) => 动态”的子类型

问题描述 投票:0回答:1

我是 Flutter 新手。我正在构建一个应用程序来显示一些文本,然后创建一个按钮来删除它。我有以下三个 dart 文件 - 1. main.dart

// ignore_for_file: prefer_const_literals_to_create_immutables, prefer_const_constructors_in_immutables,         use_key_in_widget_constructors

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

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

class QuoteList extends StatefulWidget {
  const QuoteList({super.key});

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

class QuoteListState extends State<QuoteList> {

  List<Quote> quotes = [
  Quote(text: 'hello', author: 'Pete'),
  Quote(text: 'hi', author: 'Bill'),
  Quote(text: 'hey', author: 'Tom')
  ];

  delete (Quote quote) {
    setState(() {
      quotes.remove(quote);
    });
  } 

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('quotes'),
        centerTitle: true,
        backgroundColor: Colors.redAccent,
      ),
      body: Column(
        children: quotes.map((quote) => QuoteCard(
          quote: quote,
          onRemoveData: delete(quote),
        )).toList(),
    ) );
  }
}


2。 QuoteCard.dart

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

class QuoteCard extends StatelessWidget {
  
  final Quote quote;
  final Function(Quote) onRemoveData;
  QuoteCard({required this.quote, required this.onRemoveData});
  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Text(
              quote.text,
              style: TextStyle(
                fontSize: 18.0,
                color: Colors.grey[800]
              ),
            ),
            SizedBox(height: 6.0,),
            Text(
              quote.author,
              style: TextStyle(
                fontSize: 10.0,
                color: Colors.grey[700]
              ),
            ),
            SizedBox(height: 8.0,),
            TextButton.icon(
              onPressed: onRemoveData(quote),
              label: Text('delete quote'),
              icon: Icon(Icons.delete),
            )
          ],
        ),
      ),
    );
  }
}

3.引用.dart

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

当我在 vscode 上运行应用程序时,出现此异常 发生异常 _TypeError(类型“Null”不是类型“(引用)=>动态”的子类型)

我想使用按钮删除引号,起初我创建了一个简单的删除功能,然后它给了我一个例外。我发现我需要创建一个回调(onRemoveData)。我这样做了,现在我收到这个错误

flutter dart exception callback typeerror
1个回答
0
投票

问题来自于您如何将

onRemoveData
函数传递给
onPressed
小部件
TextButton.icon
 中的 
QuoteCard

回调

尝试像这样修复它:

    TextButton.icon(
      onPressed: () { // Wrap the call in a closure
        onRemoveData(quote);
      },

如果有帮助请告诉我

© www.soinside.com 2019 - 2024. All rights reserved.