flutter BlocProvider Navigation - flutter

suppose that we navigate to "PageA" using the following code:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return BlocProvider(
create: (context) => BlocA(),
child: PageA(),
);
},
),
);
when "PageA" navigates to "PageB". how can I access "BLocA"?
I have tried following code to navigate from "PageA" to "PageB" but it crashes.
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return BlocProvider(
create: (context) => contxt.read<BlocA>(),
child: PageB(),
);
},
),
);

In order to pass an already created bloc to consequent screen you can use the BlocProvider.value your code would be like this after the change :
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return BlocProvider.value(
value: BlocProvider.of<BlocA>(context),
child: PageB(),
);
},
),
);
PageB should be able to retrieve blocA now.

Related

No instance of S present in the widget tree. Did you add S.delegate in localizationsDelegates?

I am getting this above error whenever i am trying to route to another page in flutter.
Following is my code
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyHomePage(),
));
},

Providing bloc to a new page using named route

I am trying to provide a local bloc to a new page, I found some way to do this by using an anonymous route but it doesn't look elegant
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return BlocProvider.value(
value: context.bloc<MyBloc>(),
child: NewPage());
}),
);
What I want is to do the same thing but using a named route and without creating a global bloc as I simply can't
Create app_router.dart file.
app_router.dart
class AppRouter {
final LocalBloc _localBloc = LocalBloc();
Route onGenerateRoute(RouteSettings routeSettings) {
switch (routeSettings.name) {
case '/':
return MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: _localBloc,
child: Home(),
),
);
case '/page1':
return MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: _localBloc,
child: Page1(),
),
);
case '/page2':
return MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: _localBloc,
child: Pag2(),
),
);
}
}
}
Then in your main.dart file
Add onGenerateRoute
final AppRouter _appRouter = AppRouter();
MaterialApp(
title: 'My App',
onGenerateRoute: _appRouter.onGenerateRoute,
),),
In your navigation, you can do this:
Navigator.of(context).pushNamed('/page1');

Url path not changing when navigating back to previous page in flutter web

I am working on a flutter app. i have set up router in the app with named routes. on going to my home page it shows as locahost:1234/#/home. and the path is correct. but from there when a navigate back to previouse page it still showing the same path locahost:1234/#/home. the path url is not changing on navigating back. if any one has an idea?
my router file is as follows:
/* ADD REPOSITORY TO APP ROUTER */
/* ADD REPOSITORY TO APP ROUTER */
Repository repository;
AppRouter() {
repository = new Repository(apiService: ApiService());
}
Route generateRoute(RouteSettings settings) {
switch (settings.name) {
case "/":
return MaterialPageRoute(
settings: RouteSettings(name: '/'),
builder: (_) => MultiBlocProvider(providers: [
BlocProvider<HomeCubit>(
create: (BuildContext context) {
return HomeCubit(repository: repository);
},
),
BlocProvider<SearchCubit>(
create: (BuildContext context) =>
SearchCubit(repository: repository),
),
BlocProvider<UserCubit>(
create: (BuildContext context) {
return UserCubit(repository: repository);
},
)
], child:userToken==null?SplashScreen():NavScreen()
// SplashScreen()
));
case "/getstarted":
return MaterialPageRoute(
settings: RouteSettings(name: '/getstarted'),
builder: (_) => BlocProvider(
create: (BuildContext context) =>
ProfileCubit(repository: repository),
child: OnboardingScreen()));
//return MaterialPageRoute(builder: (_) => VideoDetailScreen());
case "/register":
return MaterialPageRoute(
settings: RouteSettings(name: '/register'),
builder: (_) => MultiBlocProvider(providers: [
BlocProvider<HomeCubit>(
create: (BuildContext context) {
return HomeCubit(repository: repository);
},
),
BlocProvider<SearchCubit>(
create: (BuildContext context) =>
SearchCubit(repository: repository),
),
BlocProvider<UserCubit>(
create: (BuildContext context) =>
UserCubit(repository: repository),
)
], child: RegisterScreen()));
case '/home':
return MaterialPageRoute(
settings: RouteSettings(name: '/home'),
builder: (_) => BlocProvider(
create: (BuildContext context) =>
HomeCubit(repository: repository),
child: NavScreen()));
case '/signin':
return MaterialPageRoute(
builder: (_) => BlocProvider(
create: (BuildContext context) =>
UserCubit(repository: repository),
child: SigninScreen()),
settings: RouteSettings(name: '/signin'));
default:
return MaterialPageRoute(builder: (_) {
return Scaffold(
body: Center(
child: Text('Error! No route Found...',
style: TextStyle(color: Colors.white,fontWeight:FontWeight.bold),),
),
);
}
);
}
}
}
Just add settings: settings, to MaterialPageRoute
Page url not changing in flutter, but the page content changes fine
You have to use Flutter Navigator 2.0 to achieve Dynamic and Deep linking i.e Changing URLs on page change and getting the functionality of Browser's back and reload button.
The easiest way to achieve this is by using go_router Flutter package, which has very easy setup and documentation.
In this you can define your paths which changes on page change.

Flutter router path not showing browser tab on navigating to another page while using generate route with cubit

i am now doing a flutter project. and i use cubit for state management. for navigation i had made a file for routing.using generate route. i have given the router file code below. does anyone had an idea about this.i used block providers to give cubit and navigated to pages using named route.i works when we manually type the path in the browser tab and when we pressed back the url path is not refreshed.
class AppRouter {
/* ADD REPOSITORY TO APP ROUTER */
/* ADD REPOSITORY TO APP ROUTER */
Repository repository;
AppRouter() {
repository = new Repository(apiService: ApiService());
}
Route generateRoute(RouteSettings settings) {
switch (settings.name) {
case "/":
return MaterialPageRoute(
settings: RouteSettings(name: '/'),
builder: (_) => MultiBlocProvider(providers: [
BlocProvider<HomeCubit>(
create: (BuildContext context) {
return HomeCubit(repository: repository);
},
),
BlocProvider<UserCubit>(
create: (BuildContext context) {
return UserCubit(repository: repository);
},
)
],child:SplashScreen()
case "/register":
return MaterialPageRoute(
settings: RouteSettings(name: '/REGISTER'),
builder: (_) => MultiBlocProvider(providers: [
BlocProvider<HomeCubit>(
create: (BuildContext context) {
return HomeCubit(repository: repository);
},
),
BlocProvider<SearchCubit>(
create: (BuildContext context) =>
SearchCubit(repository: repository),
),
BlocProvider<UserCubit>(
create: (BuildContext context) =>
UserCubit(repository: repository),
)
], child: RegisterScreen()));
case '/signin':
return MaterialPageRoute(
builder: (_) => BlocProvider(
create: (BuildContext context) =>
UserCubit(repository: repository),
child: SigninScreen()),
settings: RouteSettings(name: '/signin'));
default:
return MaterialPageRoute(builder: (_) {
return Scaffold(
body: Center(
child: Text('Error! No route Found...',
style: TextStyle(color: Colors.white,fontWeight:FontWeight.bold),),
),
);
});
}
}
}

how to get the result if the pushed pageA is replaced by pageB

The question is how to get the _parkSpace in the home page?
// in home page
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FetchParkinglot(),
),
);
// in FetchParkinglot
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => BookRoute(l),
),
);
// in BookRoute
Navigator.pop(context, _parkSpace);
// in home page
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FetchParkinglot(),
),
);
// in FetchParkinglot
final result = await Navigator.push(//result is _parkSpace
context,
MaterialPageRoute(
builder: (context) => BookRoute(l),
),
);
Navigator.pop(context,result);
// in BookRoute
Navigator.pop(context, _parkSpace);