Ali Coder
Ali Coder

Reputation: 333

Unable to reflect changes while using changeNotifierProvider in flutter

devs I am working on flutter app and using provider for state management. I want to manage state of app from single class AppState but when I am using this approach changes are not reflected in UI.I am writing following code:

I want to reflect changes in ClassA while functions that increment values is calling from DummyService Class.

main.dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => SignUpBusinessLogic()),
        ChangeNotifierProvider(create: (_) => AppState()),
        ChangeNotifierProvider(
          create: (_) => AuthServices.instance(),
        ),
        StreamProvider(
          create: (context) => context.read<AuthServices>().authState,
        )
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        // initialRoute: Routes.WRAPPER_ROUTE,
        // onGenerateRoute: RouteGenerator.generateRoute,
        theme: ThemeData(
          textTheme: GoogleFonts.poppinsTextTheme(Theme.of(context).textTheme),
          scaffoldBackgroundColor: Color(0xffF0F4FD),
          buttonColor: FrontEndConfigs.appBaseColor,
          iconTheme: IconThemeData(color: FrontEndConfigs.appBaseColor),
          primaryColor: FrontEndConfigs.appBaseColor,
          floatingActionButtonTheme: FloatingActionButtonThemeData(
              backgroundColor: FrontEndConfigs.appBaseColor),
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: ClassA(),
      ),
    );
  }
}

classA.dart

class ClassA extends StatelessWidget {
  DummyService _dummyService = DummyService();
  @override
  Widget build(BuildContext context) {
    var _status = Provider.of<AppState>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("Class A"),
      ),
      body: Center(
        child: Text(_status.getA().toString()),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          _dummyService.dummyData();
        },
      ),
    );
  }
}

app_state.dart

class AppState with ChangeNotifier {

  int a = 0;

  incrementA(int i) {
    a++;
    notifyListeners();
  }

  getA() => a;
}

services.dart

class DummyService {
  AppState _appState = AppState();
  Future<void> dummyData() async {
    _appState.incrementA(0);
  }}

Upvotes: 0

Views: 58

Answers (1)

Thierry
Thierry

Reputation: 8393

In your ClassA, you create a DummyService that creates a new instance of the AppState. So, when you click on your FloatingActionButton, you are not incrementing the AppState provided by your ChangeNotifierProvider.

Try this:

class ClassA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var _status = Provider.of<AppState>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("Class A"),
      ),
      body: Center(
        child: Text(_status.getA().toString()),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          Provider.of<AppState>(context, listen: false).incrementA(0);
        },
      ),
    );
  }
}

Upvotes: 1

Related Questions