type 'List<Object?>' is not a subtype of type 'List<>' in type cast

165

In the end I figured out a bit hacky method to solve it:

var finalList = Map<String, dynamic>.from(jsonDecode(jsonEncode(data)))['SERVICESLIST'].map<BLEService>((e) => BLEService.fromMap(e)).toList();

However, to make simple I just changed the Android implementation to wrap the data using JSON with gson plugin

Share:
165
Julian Modlinski
Author by

Julian Modlinski

Updated on January 02, 2023

Comments

  • Julian Modlinski
    Julian Modlinski over 1 year

    What I'm trying to do is to communicate between Flutter and Android. For this I'm using EventChannel and MethodChannel. Due to codec restrictions I need to wrap my Android data in HashMap before sending it to Flutter. On the Flutter side I'm trying to unwrap it for display in the simple ListView. However, I have problem with typecasting that I can't figure out

    factory BLEService.fromMap(Map<String, dynamic> map) => BLEService(
        map['uuid'] as String,
        (map['characteristics'] as List<BLECharacteristic>)
            .map<BLECharacteristic>((e)  =>
          BLECharacteristic.fromMap(e as Map<String, BLECharacteristic>)
        ).toList()
      );
    

    For this line the following exception is thrown:
    type 'List<Object?>' is not a subtype of type 'List<BLECharacteristic>' in type cast

    Help to solve this issue would be greatly appreciated


    Edit
    Flutter BLECharacteristic.fromMap

    factory BLECharacteristic.fromMap(Map<String, dynamic> map) => BLECharacteristic(
        map['uuid'] as String,
      );
    

    Java BLECharacteristic.toMap

    HashMap<String, Object> toMap() {
            HashMap<String, Object> bleCharacteristicMap = new HashMap<>();
            bleCharacteristicMap.put("uuid", uuid);
            return bleCharacteristicMap;
        }
    
  • Julian Modlinski
    Julian Modlinski over 2 years
    Thanks for fast response :) I have added requested function in the original post for better visibility. In the meantime I will test your suggestion. This solution is not going to work as the there will be same typing problems with underlying HashMap
  • bharats
    bharats over 2 years
    I tried running this on DartPad and it works. Heres the link