因此,作为项目的一部分,我试图建立一个Hive数据库,储存用户交易的下列细节(除2个用户外,所有用户都将投入)
- _userID (uid generated by firebase)
- amount (value of transaction - user)
- type (Selected value from dropdown box - user)
- categoryValue (Selected value from dropdown box - user)
- DescriptionValue (String given by user)
- DateTime (DateTime.now())
the code associated is as follows
OutlinedButton(
onPressed: () {
saveTransaction(
_userID,
amount,
type,
categoryValue,
descriptionValue,
DateTime.now(),
);
这是一条单独的智能文件界定的相关法典。
void main() async {
await Hive.initFlutter();
Hive.registerAdapter(TransactionModelAdapter());
}
void saveTransaction(String userId, double amount, String type, String category, String description, DateTime dateTime) async {
// Open the Hive box for transactions
final transactionsBox = await Hive.openBox<TransactionModel>( transactions );
// Create a new transaction object
final transaction = TransactionModel()
..userId = userId
..amount = amount
..type = type
..category = category
..description = description
..dateTime = dateTime;
// Save the transaction to the box
await transactionsBox.add(transaction);
}
The following is the 方框 model in a seraparate dart file
import package:hive/hive.dart ;
part transaction_model.g.dart ; // Generated file name
@HiveType(typeId: 0) // HiveType annotation with typeId
class TransactionModel extends HiveObject {
@HiveField(0) // HiveField annotation with index
late String userId; // Unique user ID associated with the transaction
@HiveField(1) // HiveField annotation with index
late double amount;
@HiveField(2)
late String type; // Type of transaction (e.g., expense, income)
@HiveField(3)
late String category; // Category of the transaction
@HiveField(4)
late String description; // Description of the transaction
@HiveField(5)
late DateTime dateTime; // Date and time of the transaction
}
并且现在,在出现错误的地方,最终实施
void main() async {
runApp(transactionApp());
Hive.registerAdapter(TransactionModelAdapter());
await Hive.initFlutter();
}
class transactionApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: AllTransactionsPage(),
);
}
}
class AllTransactionsPage extends StatefulWidget {
@override
_AllTransactionsPageState createState() => _AllTransactionsPageState();
}
class _AllTransactionsPageState extends State<AllTransactionsPage> {
late Box<TransactionModel> transact_box;
@override
void initState() {
super.initState();
_openBox();
}
Future<void> _openBox() async {
transact_box = await Hive.openBox<TransactionModel>( transactions );
setState(() {}); // Trigger rebuild after box is initialized
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( All Transactions Page ),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// Button onPressed action
print( Button Pressed );
print("Number of entries are ${transact_box.length}");
},
child: Text( Press Me ),
),
),
);
}
}
This above code was written to check if the entries are being added to the box as desired.
在我点击纽子时,我收到以下错误信息: