我想将交易添加到卡#Flutter

i want add Transaction into Card #Flutter

我试图在单击 FlatButton 时添加列表,但我无法添加输入标题和数量 我有这个错误

Screen shot ( Error )

Simulator Screen Shot - iPhone 12 Pro Max

主文件:

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),
                            ),
                          ],
                        ),
                      ],
                    ));
              })
            ],
          ),
        ),
      ),
    );
  }
}

传递AddFun文件:

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,
            )),
      ],
    );
  }
}

我试图在按下指定按钮后向数据添加一个条目,但是没有按照我想要的方式完成?为什么这是由于列表的类型?或者我应该在代码中更改什么和什么

您正在将一个列表添加到另一个列表中。 为此,您应该使用 .addAll 而不是 .add

setState(() {
      transaction.addAll(NewList);
    });