Home > Software engineering >  Passing an Object as Navigator Pop response
Passing an Object as Navigator Pop response

Time:08-11

I'm trying to pass an object as Navigator.pop response but I get this error :

Unhandled Exception: type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, Animal>' in type cast

Here was my approach :

     Navigator.pop(context, {
            'animal': state.animal as Animal,
            'hospital': state.hospital as Hospital
          });

On route push :

    Animal? animal;
    Hospital? hospital;

    Navigator.of(context)
              .push<void>(DetailInfo.route(context))
              .then((value) {
            animal =
                (value as Map<String, Animal>)['animal'];
            hospital = (value as Map<String, Hospital>)['hospital'];
          });

CodePudding user response:

Because of Animal and Hospital are different types but they are all Object.

The solution is:

final data = value as Map<String, Object>;
animal = data['animal'] as Animal?;
hospital = data['hospital'] as Hospital?;
  • Related