Home > OS >  The argument type 'Object?' can't be assigned to the parameter type 'Celebrity&#
The argument type 'Object?' can't be assigned to the parameter type 'Celebrity&#

Time:09-01

class CelebrityListItem extends StatelessWidget {
  const CelebrityListItem({super.key, required this.celebrity});
  final Celebrity celebrity;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: GestureDetector(
        onTap: () {
          Navigator.pushNamed(
            context,
            '/celebrity',
            arguments: celebrity,
          );
        },
      ),
    );
  }
}

class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case '/':
        return MaterialPageRoute(builder: (context) => CelebrityList());
      case '/celebrity':
        return PageRouteBuilder(
          pageBuilder: (context, animation, secondaryAnimation) => CelebrityScreen(celebrity: settings.arguments),
          );
      default:
        return MaterialPageRoute(
            builder: (context) => Scaffold(
                  appBar: AppBar(
                    title: const Text("Error"),
                  ),
                  body: const Center(
                    child: Text("Page not found"),
                  ),
                ));
    }
  }
}

When I hover 'settings.arguments', this appears: RouteSettings settings Type: RouteSettings

The argument type 'Object?' can't be assigned to the parameter type 'Celebrity'.dartargument_type_not_assignable

CodePudding user response:

You should cast the arguments to the correct type.

CelebrityScreen(celebrity: settings.arguments as Celebrity)

You can also check if the arguments has the correct type by doing something like:

if(settings.arguments is Celebrity){
   // ... CelebrityScreen(celebrity: settings.arguments as Celebrity)
}

CodePudding user response:

Try this:

 class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case '/':
        return MaterialPageRoute(builder: (context) => CelebrityList());
      case '/celebrity':
         var data = settings.arguments;
        return PageRouteBuilder(
          pageBuilder: (context, animation, secondaryAnimation) => CelebrityScreen(celebrity: data),
          );
      default:
      return MaterialPageRoute(
            builder: (context) => Scaffold(
                  appBar: AppBar(
                    title: const Text("Error"),
                  ),
                  body: const Center(
                    child: Text("Page not found"),
                  ),
          ));
    }
  }
}

if this does not work, you can choose not to add it in your route generator:

Route _createRoute(Celebrity celebrity) {
    return PageRouteBuilder(
      pageBuilder: (context, animation, secondaryAnimation) => CelebrityScreen(celebrity:celebrity),
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        var begin = Offset(0.0, 1.0);
        var end = Offset.zero;
        var curve = Curves.ease;

        var tween =
            Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

        return SlideTransition(
          position: animation.drive(tween),
          child: child,
        );
      },
    );
  }

Then call like this:

 Navigator.of(context).push(_createRoute()); //Push page bottom to top
  • Related