0

I'm creating a page that displays some data from a ChangeNotifier, when I close the page (by clicking on back button) and go forward again, the data from ChangeNotifier remains the same (used in a Text). Is a ChangeNotifier a singleton? Can I make a "factory" so a new instance will be created every time?

login page

  @override
  Widget build(BuildContext context) {
    loginNotifier = context.watch<LoginNotifier>();

    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        scrolledUnderElevation: 0.0,
        backgroundColor: Colors.transparent,
        actions: [],
        elevation: 0,
      ),
      body: LayoutBuilder(builder: (context, constraint) {
        return SingleChildScrollView(
          child: ConstrainedBox(
            constraints: BoxConstraints(minHeight: constraint.maxHeight),
            child: Padding(
              padding: const EdgeInsets.all(24.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        TextField(
                          decoration: InputDecoration(
                            border: const OutlineInputBorder(),
                            labelText:
                                AppLocalizations.of(context)!.login_enter_email,
                          ),
                          controller: emailController,
                        ),
                        const SizedBox(
                          height: 24,
                        ),
                        Text(loginNotifier?.currentCountry?.localizedName ?? "") // keeps the same value of previous time
                      ]),
                ],
              ),
            ),
          ),
        );
      }),
    );
  }

App

 @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => LoginNotifier()),
      ],
      child: MaterialApp(
        title: 'App',
        theme: ThemeData(
            primarySwatch: Colors.blue,
            scaffoldBackgroundColor: Color(0xFFF7FCFC),
            useMaterial3: true,
            textTheme: kTextTheme),
        home: const HomePage(),
        localizationsDelegates: const [
          AppLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        supportedLocales: AppLocalizations.supportedLocales,
      ),
    );
  }
RafaelFerreira
  • 125
  • 1
  • 10

1 Answers1

0

A way around this is to create a function that resets the values inside your LoginNotifier class and calls the function when a user taps on a button in the page. Or wrap the scaffold with a WillPopScope and call the function. WillPopScope tells you if a user is pressing the back button.