Home > Blockchain >  Unhandled Exception: type 'String' is not a subtype of type 'DateTime' in type c
Unhandled Exception: type 'String' is not a subtype of type 'DateTime' in type c

Time:12-01

im new in flutter i want to get data from snapshot and load to screen but got this exception how can i fix it sorry for my bad english

transaction_db.dart

class TransactionDB {
  String dbName;
  TransactionDB({required this.dbName});

//HERE I TRY TO LOAD DATA FROM LOCAL DB AND GET DATA FROM LIST<DYNAMIC> IS THIS USABLE TO GET DATA AND TAKE IT TO SCREEN
Future<List<dynamic>> loaddata() async {
    var db = await opendb();
    var store = intMapStoreFactory.store('expense');
    //find = select find return list snapshot
    var snapshot = await store.find(db);
    List transactionList = <Transactions>[]; //<<< IS THIS BE LIKE COPY STRUCTOR?
    for (var record in snapshot) {
      transactionList.add(Transactions(
          title: record['title'] as String,
          subtitle: record['subtitle'] as String,
          date: record['date'] as DateTime)); //<<<<<EXCEPTION BY THIS
    }
    return transactionList;
  }
}

transaction_provider.dart

class TransactionProvider with ChangeNotifier {

  List<dynamic> transactions = [];

  void addTransaction(Transactions statement) async {
    var db = TransactionDB(
        dbName:
            'transactions.db'); 
 
    //select data to db
    transactions = await db.loaddata();

    notifyListeners();
  }
}

transaction.dart

class Transactions {
  String title ;
  String? subtitle ;
  DateTime date ;

  Transactions({required this.title,required this.subtitle,required this.date});
}
 

CodePudding user response:

Your service is returning(probably) a String type (not a DateTime type), so Dart is unable to interpretate a String into a DateTime. For doing this, instead of date: record['date'] as DateTime use date: DateTime.parse(record['date'] as String)

  • Related