How to fix this issue? I tried to add create and How do I pass these parameters?
ProxyProvider<YelloChatDb, UserDao>(
builder: (context, yelloChatDb, userdAO) => UserDao(yelloChatDb),
),
ProxyProvider<YelloVendorClient, VendorService>(
builder: (context, yelloVendorClient, categoryService) =>
VendorService.create(yelloVendorClient.chopperClient),
dispose: (context, categoryService) => categoryService.client.dispose()),
Dao class
#UseDao(tables: [Users])
class UserDao extends DatabaseAccessor<YelloChatDb> with _$UserDaoMixin {
UserDao(YelloChatDb db) : super(db);
...
}
Change log 3.2.0 https://pub.dev/packages/provider#320
Deprecated "builder" of providers in favor to "create"
You can use create instead of builder
example from https://pub.dev/packages/provider#proxyprovider
code snippet
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => Counter()),
ProxyProvider<Counter, Translations>(
create: (_, counter, __) => Translations(counter.value),
),
],
child: Foo(),
);
}
Related
I new a bloc/cubit pattern, before I used provider, getx and I want to ask something.
This application have nearly 40 pages and I want to use bloc just 10 pages and close bloc instance when is last page is closed.
Example page 1, I initialize bloc, and it is initialized on the other pages (2, 3, 4...9) will use same bloc/cubit instance and 10 page close bloc instance close
This is router class and cubit initialize here
class Router {
static Route<dynamic> generateRoute(RouteSettings settings) {
final RegisterCubit _registerCubit = RegisterCubit();
switch (settings.name) {
case welcomeRoute:
return MaterialPageRoute(builder: (_) => WelcomePage());
case loginRoute:
return MaterialPageRoute(builder: (_) => LoginPage());
case homeRoute:
return MaterialPageRoute(builder: (_) => HomePage());
case referanceCodePreview:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: ReferancaCodePreviewView(),
));
case registerOne:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageOne(),
));
case registerTwo:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageOTwo(),
));
case registerThree:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageOThree(),
));
case registerFour:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageFour(),
));
case registerFive:
return MaterialPageRoute(
builder: (a) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageFive(),
));
This page four and this page not listen to _registerCubit
class RegisterPageFour extends StatefulWidget {
RegisterPageFour({Key? key}) : super(key: key);
#override
State<RegisterPageFour> createState() => _RegisterPageFourState();
}
class _RegisterPageFourState extends State<RegisterPageFour> with
ImageSelectPicker {
#override
Widget build(BuildContext context) {
return BlocConsumer<RegisterCubit, RegisterState>(
listener: (contex, state) {},
builder: (context, state) {
return buildScaffold(context, state);
},
);
}
Navigation PageFour
CustomButton(
buttonWidth: MediaQuery.of(context).size.width,
onPress: () => Navigator.pushNamed(context, "/registerFour"),
title: "İleri"),
Can you help me this issue ?
final RegisterCubit _registerCubit = RegisterCubit();
I suggest you to create object of RegisterCubit outside of generateRoute function.
Something like this,
class Router {
final RegisterCubit _registerCubit = RegisterCubit();
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
}
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.
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),),
),
);
});
}
}
}
I'm currently working on a Flutter app and I have some issue to write tests.
When I run my test I have the following exception :
"The following ProviderNotFoundException was thrown building Consumer(dirty):
Error: Could not find the correct Provider above this Consumer
Widget"
This is a piece of my main.dart
Widget build(BuildContext context) {
themeData = Theme.of(context);
return Consumer<AppThemeNotifier>(
builder: (BuildContext context, AppThemeNotifier value, Widget? child) {
customAppTheme = AppTheme.getCustomAppTheme(value.themeMode());
return MultiBlocProvider(
providers: [
BlocProvider<AuthentificationBloc>(
create: (context) => AuthentificationBloc(),
),
BlocProvider<RegisterBloc>(
create: (context) => RegisterBloc(),
),
BlocProvider<EventsBloc>(
create: (context) => EventsBloc(),
),
BlocProvider<UsersBloc>(
create: (context) => UsersBloc(),
),
],
And this is the test that I try to run
void main() {
Widget skeleton(widgetToTest)
{
return Consumer<AppThemeNotifier>(
builder: (BuildContext context, AppThemeNotifier value, Widget? child) {
return widgetToTest;
});
}
testWidgets('My login page contains two textfield to enter my credentials',
(WidgetTester tester) async {
await tester.pumpWidget(skeleton(Login2Screen()));
final textFormFieldFinder = find.byElementType(TextFormField);
expect(textFormFieldFinder, findsNWidgets(2));
});
}
I tried to create the "skeleton" function to add the Provider but it doesn't work...
Any help is really appreciate :)
You need to load AppThemeNotifier via ChangeNotifierProvider or any other suitable provider.
await tester.pumpWidget(
ChangeNotifierProvider<AppThemeProvider>(
create: (context) => AppThemeProvider(),
child: skeleton(Login2Screen()),
),
);
The bloc that is giving me trouble is the AuthBloc. Here is how it is initialised:
void main() {
runApp(MultiBlocProvider(
providers: [
BlocProvider<AuthBloc>(
create: (_) => AuthBloc(),
),
BlocProvider<SignUpBloc>(create: (_) => SignUpBloc()),
],
child: ChangeNotifierProvider(
create: (BuildContext context) => UserModel(), child: App())));
}
Here is where I need the state to be acted on, but in certain cases the bloc's listener is not called:
Widget home(BuildContext context) {
// ignore: close_sinks
var authBloc = BlocProvider.of<AuthBloc>(context);
return BlocListener(
bloc: authBloc,
listener: (context, state) {
UserModel userModel = Provider.of<UserModel>(context, listen: false);
if (state is SignedInState) {
userModel.updateUser(state.user);
Navigator.of(context).pushReplacementNamed(kHomeRoute);
}
},
child: HomePage(),
);
}
}
I know that the bloc is recieving the event, but when I yield the state, this bloc does not rebuild. Any thoughts on where I'm going wrong?
Thanks!