How to fix Navigator not working in flutter bloc pattern? - flutter

I am trying to make an application using Pokemon api as an exercise.
I am trying to make it using the Bloc pattern.
If you click on GridTile, you need to get indexID and move to DetailPage.
Getting the IndexID is fine, but it doesn't go to the page.
I can't seem to build the page, but I don't know what the problem is.
It would be really appreciated if you could tell me what the problem is.
I put the code on github because it would be good to see the whole code.
https://github.com/Jun-Kor/pokedex.git

The main problem is not your navigation, it's your Bloc, what happens is that when you enter the PokemonDetailsView page, with the BlocBuilder<PokemonDetailsBloc, PokemonDetailsState> it cannot find its provider (the one in the main) but, if we go directly there, you have it written, right ?
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.red,
),
home: MultiBlocProvider( // here is the problem
providers: [
BlocProvider<PokemonBloc>(
create: (context) =>
PokemonBloc()..add(const PokemonPageRequestEvent(page: 0))),
BlocProvider<PokemonDetailsBloc>(
create: (context) => PokemonDetailsBloc()),
],
child: const PokedexView(),
),
);
}
}
Well, the MultiBlocProvider has to be the main parent that is going to provide for the whole MaterialApp, not be a child of it, I mean ? That's why the problem was asking for a Bloc provider, but I
it couldn't find one.
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MultiBlocProvider( // change here
providers: [
BlocProvider<PokemonBloc>(
create: (context) =>
PokemonBloc()..add(const PokemonPageRequestEvent(page: 0))),
BlocProvider<PokemonDetailsBloc>(
create: (context) => PokemonDetailsBloc()),
],
child: MaterialApp(
title: 'Material App',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.red,
),
home: const PokedexView(),
),
);
}
}

Related

How do I handle two packages that require MaterialApp as child in flutter?

I am using this screen util package that helps handle the responsiveness of the app on different screen size and I am also using provider both of them require MaterialApp as child but I don't know how to go about it instead I did this and now I'm having some issues navigating in the app.
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(providers: [
ChangeNotifierProvider(create: (_)=>
TestimonyVeiwModel()
),
],
child: ScreenUtilInit(
builder:() => MaterialApp(
title: 'Stea app',
theme: ThemeData();
I prefer using this way
return ScreenUtilInit(
builder: () => MultiProvider(
providers: [...],
child: MaterialApp(
//...
),
),
);

Using more than one provider in a flutter app

I have started using PROVIDER to manage state in my app. I followed tutorials and wrapped my Material app with ChangeNotifierProvider.
Here's the code :
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (BuildContext context) => ListsProvider(),
child: MaterialApp(
title: 'WordsApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: StartingPage.id,
routes: {
StartingPage.id: (context) => StartingPage(),
RegistrationScreen.id: (context) => RegistrationScreen(),
},
),
);
}
}
This provider called "ListsProvider" takes care of "providing" lists that need to be displayed on different screens.
I have now created a second provider which I called "user_data_provider" and I now need to add it to my app too. It will take care of providing user data to many different screens.
How can I do that ?
To achive this you can use Multiprovider as shown below
Add this to the top of your app. If you need these obj everywhere.
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<user_data_provider>(
create: (_) => user_data_provider(),
),
ChangeNotifierProvider<ListsProvider>(
create: (_) => ListsProvider(),
),
],
child: Builder(
builder: (BuildContext context) {
return MaterialApp(
//YOur code goes here
);
},
),
);

How do have multiple provider in changenotifierprovider?

I'm new to Flutter. I have encountered the following problem where I have no idea how to add another changenotifierprovider to my app.dart. Before that I already have the EntryProvider(), Now I wish to add another provider called EnterProvider(). Below is my coding for app.dart:
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => EntryProvider(),
child: MaterialApp(
home: WelcomeBackPage(),
theme: ThemeData(
accentColor: Colors.orangeAccent,
primaryColor: Colors.black,
textTheme: GoogleFonts.openSansTextTheme(),
),
debugShowCheckedModeBanner: false,),
);
}
}
Could anyone help me out on this?
You can try MultiProvider .
MultiProvider(
providers: [
Provider<Something>(create: (_) => Something()),
Provider<SomethingElse>(create: (_) => SomethingElse()),
Provider<AnotherThing>(create: (_) => AnotherThing()),
],
child: someWidget,
)

Provider Error: Could not find the correct Provider<Color> above this MyApp Widget

I do not understand why the following code generates the error?
According to me the provider is an ancestor to MaterialApp and it should work? What am I missing?
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
new Provider<Color>(create: (_) => Colors.amber)
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Provider.of<Color>(context),
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
)
);
}
}
Provider initially in build method. You don't access to use provider value because provider inits context, but you tried to use provider.of(context) in the same place. If you use another state(view), you should use it.
Example code : https://gist.github.com/VB10/358dc944e5a86b58db2d898eaa1b5ea6

Can I wrap the MaterialApp widget with the Provider?

I'm new to flutter and I'm trying to create an app with provider. I wrapped MaterialApp widget with the ChangeNotifierProvider and the app works and I can use the provider as it intended to do. I need to know is it okay to do so and will i face any problems?
Widget build(BuildContext context) {
return ChangeNotifierProvider<BaseModel>(
builder: (context) =>
BaseModel(loading: false, title: "Title", isLoggedIn: false),
child: MaterialApp(
routes: <String, WidgetBuilder>{
"/home": (BuildContext context) => Home(),
"/signIn": (BuildContext context) => SignIn()
},
initialRoute: "/signIn",
title: 'Flutter Demo',
theme: ThemeData(
// is not restarted.
primarySwatch: Colors.blue,
),
home: SignIn()),
);
In all the sample codes they use Provider under "home" in MaterialApp widget. I used MaterialApp inside the provider.
It is totally fine. There's no problem whatsoever.