Home > Enterprise >  Unhandled Exception: type 'List<dynamic>'
Unhandled Exception: type 'List<dynamic>'

Time:08-17

I'm trying to recieve a list from a Sql Api. The catch is that i need to give an id with the query. the Widget.klant.klantId has the value i need. i know it has somthing to do with the as List<Machine> in accountpage.dart. Hope you can help me with this problem. thanks in advance.

The hole error: enter image description here

accountpage.dart:

class Accountpage extends StatefulWidget {
  const Accountpage(this.klant);

  final Klant klant;

  @override
  _AccountpageState createState() => _AccountpageState();
}

class _AccountpageState extends State<Accountpage> {
  _AccountpageState();

  final ApiService api = ApiService();
  late List<Machine> machineList;

  @override initState(){
    super.initState();
    _getMachine();
    machineList = [];
  }

  void _getMachine() async{
    machineList = (await ApiService().getMoreMachine(widget.klant.klantId.toString())) as List<Machine>;
    Future.delayed(const Duration(seconds: 1)).then((value) => setState(() {}));
  }

  @override
  Widget build(BuildContext context) {

  //Here starts the body

api_machine.dart:

  Future<Machine> getMoreMachine(String klantId) async {
    final response = await get(Uri.parse('$apiUrl/Select/$klantId'));

    if (response.statusCode == 200) {
      return Machine.fromJson(json.decode(response.body));
    } else {
      throw Exception('Failed to load a case');
    }
  }

MachineModel.dart:

List<Machine> welcomeFromJson(String str) => List<Machine>.from(json.decode(str).map((x) => Machine.fromJson(x)));

String welcomeToJson(List<Machine> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Machine {
  Machine({
    this.serienummerId,
    this.serienummer,
    this.bouwjaar,
    this.urenstand,
    this.locatie,
    this.klantId,
  });

  int? serienummerId;
  String? serienummer;
  String? bouwjaar;
  String? urenstand;
  String? locatie;
  String? klantId;

  factory Machine.fromJson(Map<String, dynamic> json) => Machine(
    serienummerId: json["SerienummerId"],
    serienummer: json["Serienummer"],
    bouwjaar: json["Bouwjaar"],
    urenstand: json["Urenstand"],
    locatie: json["Locatie"],
    klantId: json["KlantId"],
  );

  Map<String, dynamic> toJson() => {
    "SerienummerId": serienummerId,
    "Serienummer": serienummer,
    "Bouwjaar": bouwjaar,
    "Urenstand": urenstand,
    "Locatie": locatie,
    "KlantId": klantId,
  };
}

json result

[
  {
    "SerienummerId": 1,
    "Serienummer": "-----",
    "Bouwjaar": "2020",
    "Urenstand": "10",
    "Locatie": "---",
    "KlantId": "1"
  },
  {
    "SerienummerId": 2,
    "Serienummer": "-----",
    "Bouwjaar": "1998",
    "Urenstand": "5010",
    "Locatie": "----",
    "KlantId": "1"
  }
]

CodePudding user response:

You are parsing the result as if it's a single Machine while it in fact is a list of machines. Process it as a list and also use the correct return type accordingly. Like

Future<List<Machine>> getMoreMachine(String klantId) async {
  final response = await get(Uri.parse('$apiUrl/Select/$klantId'));

  if (response.statusCode == 200) {
    return List<Machine>.from(json.decode(response.body).map((x) => Machine.fromJson(x)));
  } else {
    throw Exception('Failed to load a case');
  }
}

CodePudding user response:

the return type of the method is Machine:

 Future<Machine> getMoreMachine(String klantId) async {
    final response = await get(Uri.parse('$apiUrl/Select/$klantId'));

    if (response.statusCode == 200) {
      return Machine.fromJson(json.decode(response.body));
    } else {
      throw Exception('Failed to load a case');
    }
  }

and then you cast a Machine to List :

machineList = (await ApiService()
.getMoreMachine(widget.klant.klantId.toString())) as List<Machine>;

I don't know what the JSON looks like... but if there is only one machine you could for example add it to a list like this:

machineList.add((await ApiService()
.getMoreMachine(widget.klant.klantId.toString())));

Update

Try this:

Future<List<Machine>> getMoreMachine(String klantId) async {
    final response = await get(Uri.parse('$apiUrl/Select/$klantId'));

    if (response.statusCode == 200) {
      final jsonMachines = Machine.fromJson(json.decode(response.body));
      return jsonMachines.map((item) => Machine.fromJson(item)).toList();
    } else {
      throw Exception('Failed to load a case');
    }
  }

CodePudding user response:

I think this is because of in getMoreMachine you used return type as Machine actually you are assigning that value as List so make that change like this :

  Future<Machine> getMoreMachine(String klantId) async {
    final response = await get(Uri.parse('$apiUrl/Select/$klantId'));

    if (response.statusCode == 200) {
      return welcomeFromJson(response.body);
    } else {
      throw Exception('Failed to load a case');
    }
  }

might be other think is you can check your API response that is not returning List of machines.

  • Related