Home > Software design >  How to define and pass a parameter from ChangeNotifierProvider to a child class
How to define and pass a parameter from ChangeNotifierProvider to a child class

Time:05-04

I need some help please with my code. I repeat myself and I want to fix this issue on line 9 and line 12 where I call the same constructor twice. Is somehow possible to do the call only once and to store the call of LocalAuthService(LoginWebApi(WebAccess.LOGIN_ENDPOINT), prefs, packageInfo.version) into a parameter and to pass it to the MainPage constructor ? If the answer is yes, how will be that possible please ?

Here is my code:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  PackageInfo packageInfo = await PackageInfo.fromPlatform();

  SharedPreferences.getInstance().then((prefs) async {
    runApp(
      ChangeNotifierProvider(
        create: (_) => LocalAuthService(LoginWebApi(WebAccess.LOGIN_ENDPOINT), prefs, packageInfo.version),
        child: MainPage(
          initialRoute: isLogged ? '/home' : '/',
          authService: LocalAuthService(LoginWebApi(WebAccess.LOGIN_ENDPOINT), prefs, packageInfo.version),
        ),
      ),
    );
  });
}

class MainPage extends StatefulWidget {
  final String initialRoute;
  final LocalAuthService authService;

  const MainPage(
      {Key? key, required this.initialRoute, required this.authService})
      : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  final String appName = 'My App';

  int currentIndex = 0;

  late final screens = [
    HomeScreen(authService: widget.authService),
    SecondScreen(),
    ThirdScreen(),
    FourthScreen()
  ];
}

When I run the app obviously because the constructor is called twice the console looks like this:

enter image description here

Thanks for reading this !

CodePudding user response:

The problem is that when you call a constructor of a class - new unique object is created every time. So the main use-case of ChangeNotifierProvider is creating some singleton object available by the context throughout the application. It means - you do not have to put it twice for a MainPage, but access it with Selector or Consumer: ....

late final screens = [
Consumer<LoginAuthService>(builder:(context, service, _) => HomeScreen(authService: service )),
SecondScreen(),
ThirdScreen(),
FourthScreen()];

.... And you may remove the parameter from MainPage's constructor

  • Related