Anton Schrage
Anton Schrage

Reputation: 1301

How to fix type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<dynamic>' in type cast

i created a method and when i built it, this error comes:

type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<dynamic>' in type cast

Then i deleted the method and the error is still there.

Here my Code:

class Produkt{
  String name;
  int anzahl;

  Produkt({
    this.name,
    this.anzahl,
  });

  factory Produkt.fromJson(Map<String, dynamic> parsedJson){
    return Produkt(
        name:parsedJson['Name'],
        anzahl:parsedJson['Anzahl']
    );
  }

  Map<String, dynamic> toProduktJson() =>
  {
    "Name" : name,
    "Anzahl" : anzahl
  };
}

Class ProduktList

class ProduktList{
  final List<Produkt> produkte;

  ProduktList({
    this.produkte,
  });

  factory ProduktList.fromJson(Map<String, dynamic> parsedJson){
    var list = parsedJson["Produkte"] as List;
    List<Produkt> produkte = list.map((i) => Produkt.fromJson(i.cast<String, dynamic>())).toList();

    return ProduktList(
        produkte: produkte,
    );
  }

  Map<String, dynamic> toProdukteJson() =>
  {
    "Produkte" : [
      produkte[0].toProduktJson(),
      produkte[1].toProduktJson(),
    ]
  };
}

I think the error comes from here, but i am not sure:

datenUebertragen(int index, AsyncSnapshot<QuerySnapshot> snapshot, ProduktList produkte){

    Firestore.instance.runTransaction((transaction) async{
      await transaction.update(
      Firestore.instance.collection("Benutzer").document("Anton").collection("Einkaufsliste").document(
        snapshot.data.documents[index].documentID),
        produkte.toProdukteJson()
      );
  }
);
 }

I call this method on an IconButton onPressed. Can anyone help me?

Thank you

EDIT:

I call it like this:

ProduktList produkte = new ProduktList.fromJson(snapshot.data.documents[index].data);

the data look like:

{Produkte: [{Anzahl: 201, Name: Zucker}, {Anzahl: 10, Name: Backpulver}]}

EDIT:

enter image description here

It looks like this!

EDIT:

enter image description here

Upvotes: 2

Views: 95

Answers (1)

diegoveloper
diegoveloper

Reputation: 103421

try changing this :

 var list = parsedJson["Produkte"] as List;

to this :

 final List list = parsedJson["Produkte"] as List;

Upvotes: 1

Related Questions