i want add Transaction into Card #Flutter
213
You're adding a List into another List. To do this, you should use .addAll instead of just .add
setState(() {
transaction.addAll(NewList);
});
Author by
Moe
Updated on December 26, 2022Comments
-
Moe over 1 year
i am trying to add list when i click FlatButton But i can't added input title and amount i have this Error
Simulator Screen Shot - iPhone 12 Pro Max
MainFile:
import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:testspeed3/passAddfun.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { final titleController = TextEditingController(); final amountController = TextEditingController(); final List transaction = [ { 'title': 'Home', 'amount': 44, 'date': DateTime.now(), }, // { // 'title': 'Muhammed', // 'amount': 22, // 'date': DateTime.now(), // }, // { // 'title': 'BestPR', // 'amount': 11, // 'date': DateTime.now(), // }, ]; void SubmitData(var titleSub, double amountsub) { final NewList = [ { 'title': titleSub, 'amount': amountsub, 'date': '${DateTime.now()}', } ]; setState(() { transaction.add(NewList); }); } @override Widget build(BuildContext context) { return MaterialApp( title: 'Test Speeeed', home: Scaffold( appBar: AppBar( title: Text('Home'), ), body: Container( margin: EdgeInsets.all(20), child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ PassData(SubmitData), ...(transaction).map((e) { return Card( margin: EdgeInsets.fromLTRB(0, 10, 0, 10), elevation: 3, child: Row( children: [ Container( padding: EdgeInsets.all(3), margin: EdgeInsets.all(20), decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.blue, ), child: Text( '\$${e['amount']}', style: TextStyle(fontSize: 15, color: Colors.white), )), Column( children: [ Container( margin: EdgeInsets.fromLTRB(0, 0, 0, 10), child: Text( e['title'], style: TextStyle(fontWeight: FontWeight.bold), )), Text( '${DateFormat.MMMMd().format(e['date'])}', style: TextStyle(color: Colors.black38), ), ], ), ], )); }) ], ), ), ), ); } }
PassAddFun File:
import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; class PassData extends StatefulWidget { final Function Adx; PassData(this.Adx); @override _PassDataState createState() => _PassDataState(); } class _PassDataState extends State<PassData> { final titleController = TextEditingController(); final amountController = TextEditingController(); void sumbitdata() { widget.Adx(titleController.text, double.parse(amountController.text)); } @override Widget build(BuildContext build) { return Column( children: [ Container( padding: EdgeInsets.fromLTRB(0, 0, 0, 30), child: TextField( decoration: InputDecoration(labelText: 'Title'), controller: titleController, )), Container( child: TextField( decoration: InputDecoration(labelText: 'Amount'), controller: amountController), ), Container( margin: EdgeInsets.fromLTRB(0, 10, 0, 40), child: FlatButton( onPressed: () { sumbitdata(); }, child: Text( 'Add Transaction', style: TextStyle(color: Colors.white), ), color: Colors.blue, )), ], ); } }
I am trying to add an entry to the data after pressing the specified button, but it is not done the way I want it? Why is this due to the type of List? Or what and what should I change in the code