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),
),
),
);
}
}
2
Answers
If you like assign widget variable on state class, it will be
or just
from reading your code, I can say that you forget to assign the value to the transaction, instead of this:
change it with this: