Home > front end >  the argument type 'string' can't be assigned to the parameter type 'list widget
the argument type 'string' can't be assigned to the parameter type 'list widget

Time:12-06

how to get my data in the transaction class to present it in my wedget? it keep showing "the argument type 'string' can not be assign to parameter type'list'. how do i solve this problem? i keep facing problem with map() function. since im beginner in this field can any one please enlighten me

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:taskmangment/transaction.dart';
import './transaction.dart';

void main(List<String> args) {
  runApp(taskmangment());
}

class taskmangment extends StatelessWidget {
  //list of data from backend
  final List<Transaction> transactions = [
    Transaction(id: 't1', titel: 'new shoe', amount: 5, date: DateTime.now()),
    Transaction(
        id: 't2', titel: 'weekly Groceries', amount: 2, date: DateTime.now())
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text('first application')),
      body: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Container(
              width: double.infinity,
              height: 100,
              child: Card(
                color: Colors.blueAccent,
                elevation: 5,
                child: Text('chart!'),
              ),
            ),
            Column(
              children: transactions.map((tx) {
                return Card(
                  child: Text(tx.titel),
                );
              }).toString(),
            )
          ]),
    ));
  }
}

// this is my transaction class

class Transaction {
  String id;
  String titel;
  double amount;
  DateTime date;

  Transaction(
      {required this.id,
      required this.titel,
      required this.amount,
      required this.date});
}

CodePudding user response:

You need to replace toString() with toList(), like this:

Column(
      children: transactions.map((tx) {
        return Card(
          child: Text(tx.titel),
        );
      }).toList(),
    )
  • Related