Home > Software design >  Getting the error: 'DateTime' is not a subtype of type 'String'
Getting the error: 'DateTime' is not a subtype of type 'String'

Time:11-26

import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:flutter_complete_guide/models/transaction.dart';
import 'package:intl/intl.dart';

class Chart extends StatelessWidget {
  final List<Transaction> recentTransactions;

  Chart(this.recentTransactions);
  List<Map<String, Object>> get groupedTransactionValues {
    return List.generate(7, (index) {
      final weekDay = DateTime.now().subtract(Duration(days: index));
      var totalSum = 0.0;

      for (var i = 0; i < recentTransactions.length; i  ) {
        if (recentTransactions[i].date.day == weekDay.day &&
            recentTransactions[i].date.month == weekDay.month &&
            recentTransactions[i].date.year == weekDay.year) {
          totalSum  = recentTransactions[i].amount;
        }
      }

      print(DateFormat.E().format(weekDay).substring(0, 1));
      print(totalSum);

      return {'day': DateFormat.E(weekDay), 'amount': totalSum};
    });
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.all(10),
      elevation: 5,
      child: Row(
        children: groupedTransactionValues.map((data) {   //Error shows just after writing this
          return Text(data['day'].toString()   ':'   data['amount'].toString());
        }).toList(),
      ),
    );
  }
}

I dont know what I am doing wrong

I tried going through the code again and again but wasnt able to figure out why it was giving the error when I return that list created by map to the children.

CodePudding user response:

The error is on this line:

  return {'day': DateFormat.E(weekDay), 'amount': totalSum};

You should write

  return {'day': DateFormat.E().format(weekDay), 'amount': totalSum};

Just like the way you used DateFormat in the print two lines above it

  • Related