Home > database >  LateInitializationError: Field 'transaction' has not been initialized
LateInitializationError: Field 'transaction' has not been initialized

Time:01-03

Can u please check the both widgets and let me know where is the problem actually? it's been showing me that the transaction hasn't been initialized. in the first widget i've indicated the problem location by ( problem here ) in ListView. Chcek both widget please.

this is transaction_list.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(),
          );
  }
}

// ignore_for_file: deprecated_member_use this is transaction_item.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),
              ),
      ),
    );
  }
}

CodePudding user response:

If you like assign widget variable on state class, it will be

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

or just

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

CodePudding user response:

from reading your code, I can say that you forget to assign the value to the transaction, instead of this:

  late final Transaction? transaction;

change it with this:

  final Transaction? transaction = widget.transaction;
  • Related