How to rebuild the root widget of flutter?

1k views Asked by At

I am using provider package to detect changes in 'todaysTasks' but as you can see 'todayTasks' depends on DaysRecords which stores a list of DayTasks, so what I want is to rebuild my root widget (which is MyApp) whenever i push a DayTask in DaysRecords.recordsList but can't figure out any way to do that. Is there any solution to this??

class MyApp extends StatelessWidget {
  static const routeName = '/root';
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    DayTasks todaysTasks = DaysRecords.recordsList.last;
    return ChangeNotifierProvider(
      create: (context) => todaysTasks,
1

There are 1 answers

3
Omar Khaium Chowdhury On

You can just wrap the parent widget of MyApp (I'm guessing, it is MaterialApp on main method) with ChangeNotifierProvider instead of wrapping inside MyApp.

Then initialize the correspondent provider in MyApp's build method or use a consumer to rebuild (manage your state).

void main() {
  runApp(MaterialApp(
    home: RootWidget(),
  ));
}

class RootWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => SomeProvider(),
      child: MyApp(),
    );
  }
}

class MyApp extends StatelessWidget {
  static const routeName = '/root';

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<SomeProvider>(context, listen: true);

    // DayTasks todaysTasks = provider.recordsList.last;
    return Scaffold(
      body: AppBar(
        automaticallyImplyLeading: false,
      ),
    );
  }
}