MKapp
MKapp

Reputation: 537

Exception type 'List<dynamic>' is not a subtype of type 'List<int>' in type cast in a Flutter App

I am getting the following exception when I run the main.dart file inside a flutter app. The code is reading data from a data.json file and mapping it into a SubTypeMap class and then to a TypeMap class. Can anyone suggest why this is happening and a solution to this issue?

exception:

[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<int>' in type cast
#0      new TypeMap.fromJson (package:newapp/main.dart:82:30)

assets/data.json

[
    {
      "type1": 1,
      "type2": 1,
      "type3": [1, 2, 3, 4],
      "type4": [
        { "subtype1": 1, "subtype2": "lorem", "subtype3": "lorem" },
        { "subtype1": 2, "subtype2": "lorem", "subtype3": "lorem" },
        { "subtype1": 3, "subtype2": "lorem", "subtype3": "lorem" },
        { "subtype1": 4, "subtype2": "lorem", "subtype3": "lorem" }
      ]
    },
    {
      "type1": 2,
      "type2": 2,
      "type3": [5, 6, 7, 8],
      "type4": [
        { "subtype1": 5, "subtype2": "lorem", "subtype3": "lorem" },
        { "subtype1": 6, "subtype2": "lorem", "subtype3": "lorem" },
        { "subtype1": 7, "subtype2": "lorem", "subtype3": "lorem" },
        { "subtype1": 8, "subtype2": "lorem", "subtype3": "lorem" }
      ]
    }
  ]
  

main.dart

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  List<TypeMap> maps;

  Future<void> fetchMap() async {
    final parsedJson =
        jsonDecode(await rootBundle.loadString('assets/data.json'))
            as List<dynamic>;
    print('parsed Json: $parsedJson');
    maps = parsedJson.map<TypeMap>((item) => TypeMap.fromJson(item)).toList();
    print('maps: ${maps}');
    print('maps first: ${maps.first}');
  }

  @override
  void initState() {
    // TODO: implement initState
    fetchMap();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(child: Text('Hello')),
    );
  }
}

class TypeMap {
  final int type1;
  final int type2;
  final List<int> type3;
  final List<SubTypeMap> type4;

  TypeMap({this.type1, this.type2, this.type3, this.type4});

  factory TypeMap.fromJson(Map<String, dynamic> json) {
    // json['reward']
    final newList = json['type4'] as List<dynamic>;
    final updatedList =
        newList.map<SubTypeMap>((item) => SubTypeMap.fromJson(item)).toList();
    print('updated List : ${updatedList}');

    return TypeMap(
        type1: json['type1'] as int,
        type2: json['type2'] as int,
        type3: json['type3'] as List<int>,
        type4: updatedList);
  }
}

class SubTypeMap {
  final int subtype1;
  final String subtype2;
  final String subtype3;

  SubTypeMap({this.subtype1, this.subtype2, this.subtype3});

  factory SubTypeMap.fromJson(Map<String, dynamic> json) {
    return SubTypeMap(
      subtype1: json['subtype1'] as int,
      subtype2: json['subtype2'] as String,
      subtype3: json['subtype3'] as String,
    );
  }
}

Upvotes: 0

Views: 206

Answers (1)

blackkara
blackkara

Reputation: 5052

It must be cast instead of as List<int> below

 return TypeMap(
    type1: json['type1'] as int,
    type2: json['type2'] as int,
    type3: json['type3'].cast<int>(), // Not as List<int>
    type4: updatedList);

Upvotes: 2

Related Questions