Home > Blockchain >  Flutter IOS _CastError
Flutter IOS _CastError

Time:12-01

I am currently developing a flutter app. I have tested a feature on Android and there everything worked, when I test it on IOS, unfortunately the following error is called:

_CastError (type 'Contract' is not a subtype of type 'Damage' in type cast)

The code to which the error refers is the following:

final Damage damage = item as Damage;
return Scaffold(
  floatingActionButton: FloatingActionButton(
    tooltip: 'Add',
    onPressed: () {
      Navigator.of(context).push(
        MaterialPageRoute(
          builder: (BuildContext context) => AddDocumentsPage(
            damage: damage,
          ),
        ),
      );
    },
    child: Icon(Icons.add),
  ),
  body: DocumentsList(
    attribute: 'date',
    documentable: item,
  ),
);

CodePudding user response:

You should be very cautious when using the as keyword. It is very likely that your item isn't of type Damage and therefore cannot be cast into one. Your best option here would be to create a new Damage object and fulfilling it with the data you need from item.

  • Related