无法将参数类型 'Object?' 分配给参数类型 'EditProfileScreenArgs?
The argument type 'Object?' can't be assigned to the parameter type 'EditProfileScreenArgs?
我正在使用 Flutter 和 Block StateManagement
当我将参数传递给路由时发生错误,它给了我这个错误:
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'),
),
);
}
}
您需要指明传递参数的类型,否则它将被视为 Object?
类型的泛型。在 custom_router
.
的 onGenerateNestedRout
方法中尝试此代码
switch (settings.name) {
case EditProfileScreen.routeName:
return EditProfileScreen.route(args: settings.arguments as EditProfileScreenArgs?);
default:
return _errorRoute();
}
我正在使用 Flutter 和 Block StateManagement
当我将参数传递给路由时发生错误,它给了我这个错误:
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'),
),
);
}
}
您需要指明传递参数的类型,否则它将被视为 Object?
类型的泛型。在 custom_router
.
onGenerateNestedRout
方法中尝试此代码
switch (settings.name) {
case EditProfileScreen.routeName:
return EditProfileScreen.route(args: settings.arguments as EditProfileScreenArgs?);
default:
return _errorRoute();
}