I'm using Flutter and Block StateManagement
The error occurred when I passed an argument to a route and it gives me this error :
The argument type 'Object?' can't be assigned to the parameter type 'EditProfileScreenArgs?
you can see the error in custom_router.dart
custom_router.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_insta_bloc_firebase/screens/screens.dart';
class CustomRouter {
static Route onGenerateRout(RouteSettings settings) {
print('Route : ${settings.name}');
switch (settings.name) {
case '/':
return MaterialPageRoute(
settings: const RouteSettings(name: '/'),
builder: (_) => Scaffold());
case SplashScreen.routeName:
return SplashScreen.route();
case LoginScreen.routeName:
return LoginScreen.route();
case NavScreen.routeName:
return NavScreen.route();
default:
return _errorRoute();
}
}
static Route onGenerateNestedRout(RouteSettings settings) {
print('Nested Route : ${settings.name}');
switch (settings.name) {
case EditProfileScreen.routeName:
return EditProfileScreen.route(args: settings.arguments);
default:
return _errorRoute();
}
}
static Route _errorRoute() {
return MaterialPageRoute(
settings: const RouteSettings(name: '/error'),
builder: (_) => Scaffold(
appBar: AppBar(
title: const Text('Error'),
),
body: const Center(
child: Text('Some Thing went wrong!'),
),
));
}
}
profile_button.dart
// ignore_for_file: deprecated_member_use
import 'package:flutter/material.dart';
import 'package:flutter_insta_bloc_firebase/screens/screens.dart';
class ProfileButton extends StatelessWidget {
final bool isCurrentUser;
final bool isFollowing;
const ProfileButton(
{Key? key, required this.isCurrentUser, required this.isFollowing})
: super(key: key);
@override
Widget build(BuildContext context) {
return isCurrentUser
? FlatButton(
color:
isFollowing ? Colors.grey[300] : Theme.of(context).primaryColor,
onPressed: () => Navigator.of(context).pushNamed(
EditProfileScreen.routeName,
arguments: EditProfileScreenArgs(
context: context)), //send context for editProfileScreen
child: Text(
'Edite Profile',
style: TextStyle(
color: isFollowing ? Colors.black : Colors.white,
fontSize: 16.0),
))
: FlatButton(
color:
isFollowing ? Colors.grey[300] : Theme.of(context).primaryColor,
onPressed: () {},
child: Text(
isFollowing ? 'Unfollow' : 'Follow',
style: TextStyle(
color: isFollowing ? Colors.black : Colors.white,
fontSize: 16.0),
));
}
}
Edit_Profile_screen.dart
import 'dart:js';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_insta_bloc_firebase/repository/repository.dart';
import 'package:flutter_insta_bloc_firebase/screens/edit_profile/cubit/edite_profile_cubit.dart';
import 'package:flutter_insta_bloc_firebase/screens/pages/profile/bloc/profile_bloc.dart';
import 'package:meta/meta.dart';
//with this class we can pass argumentfor go to profile_btn
class EditProfileScreenArgs {
final BuildContext context;
const EditProfileScreenArgs({required this.context});
}
class EditProfileScreen extends StatelessWidget {
static const String routeName = '/editProfile';
static Route route({EditProfileScreenArgs? args}) {
return MaterialPageRoute(
settings: const RouteSettings(name: routeName),
builder: (context) => BlocProvider<EditeProfileCubit>(
create: (context) => EditeProfileCubit(
userReposirory: context.read<UserReposirory>(),
storageRepository: context.read<StorageRepository>(),
profileBloc: args!.context.read<ProfileBloc>()),
child: EditProfileScreen(),
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Edit Profile'),
),
);
}
}
CodePudding user response:
You need to tell the type of the passing argument otherwise it will be consider as generic of type Object?
. Try this code in onGenerateNestedRout
method of your custom_router
.
switch (settings.name) {
case EditProfileScreen.routeName:
return EditProfileScreen.route(args: settings.arguments as EditProfileScreenArgs?);
default:
return _errorRoute();
}