flutter 延迟初始化错误:字段'transaction'尚未初始化

tnkciper  于 2023-01-06  发布在  Flutter
关注(0)|答案(2)|浏览(292)

你可以请检查这两个小部件,让我知道问题到底在哪里?它一直显示我的交易还没有初始化。在第一个小部件中,我已经指出了问题的位置(问题在这里)在列表视图。请检查这两个小部件。
这是事务列表.dart

import 'package:flutter/material.dart';
import '../widgets/transaction_item.dart';
import '../models/transaction.dart';
import 'package:intl/intl.dart';

class TransactionList extends StatelessWidget {
  // TransactionList({Key? key}) : super(key: key);

  final List<Transaction> transactions;
  final Function deleteTx;

  TransactionList(this.transactions, this.deleteTx);

  @override
  Widget build(BuildContext context) {
    return transactions.isEmpty
        ? LayoutBuilder(
            builder: (context, Constraints) {
              return Column(
                children: [
                  Text(
                    'No transaction added yet!',
                    style: Theme.of(context).textTheme.subtitle2,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  Container(
                    height: Constraints.maxHeight * 0.6,
                    child: Image.asset(
                      'assets/images/waiting.png',
                      fit: BoxFit.cover,
                    ),
                  ),
                ],
              );
            },
          )
        : ListView(
            children: transactions
                .map(
                  (tx) => (problem here) TransactionItem(
                      key: ValueKey(tx.id),
                      transaction: tx,
                      deleteTx: deleteTx),
                )
                .toList(),
          );
  }
}

//忽略文件:已弃用的成员使用这是事务项.dart

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../models/transaction.dart';

class TransactionItem extends StatefulWidget {
  const TransactionItem({
    Key? key,
    @required this.transaction,
    required this.deleteTx,
  }) : super(key: key);

  final Transaction? transaction;
  final Function deleteTx;

  @override
  State<TransactionItem> createState() => _TransactionItemState();
}

class _TransactionItemState extends State<TransactionItem> {
  late final Transaction? transaction;
  late final Function deleteTx;

  late Color _bgColor;

  @override
  void initState() {
    const availableColors = [
      Colors.red,
      Colors.blue,
      Colors.pink,
      Colors.purple,
    ];

    _bgColor = availableColors[Random().nextInt(4)];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 5, vertical: 8),
      elevation: 5,
      child: ListTile(
        leading: CircleAvatar(
          backgroundColor: _bgColor,
          radius: 33,
          child: Padding(
            padding: const EdgeInsets.all(6),
            child: FittedBox(
              child: Text('\$ ${transaction?.price}'),
            ),
          ),
        ),
        title: Text(
          transaction!.title,
          style: Theme.of(context).textTheme.titleSmall,
        ),
        subtitle: Text(
          DateFormat.yMMMd().format(transaction!.date),
        ),
        trailing: MediaQuery.of(context).size.width > 460
            ? FlatButton.icon(
                onPressed: () => deleteTx(transaction!.id),
                icon: const Icon(Icons.delete),
                label: const Text('Delete'),
                textColor: Theme.of(context).errorColor,
              ) 
            : IconButton(
                //to delete the transactions
                icon: const Icon(Icons.delete),
                color: Theme.of(context).errorColor,
                onPressed: () => deleteTx(transaction!.id),
              ),
      ),
    );
  }
}
jq6vz3qz

jq6vz3qz1#

如果你喜欢在状态类上分配小部件变量,它将是

class _TransactionItemState extends State<TransactionItem> {
  Transaction? transaction;
  .....
  @override
  void initState() {
    transaction = widget.transaction; //this

或者只是

class _TransactionItemState extends State<TransactionItem> {
  late Transaction? transaction = widget.transaction;
w1jd8yoj

w1jd8yoj2#

通过阅读您的代码,我可以说您忘记为事务赋值,而不是这样:

late final Transaction? transaction;

使用以下命令更改它:

final Transaction? transaction = widget.transaction;

相关问题