Mala
Mala

Reputation: 99

How can other page be updated with provider value?

How can the other page be updated with the provider value given from the main tab page? The progress value is passed to second tab using provider. The secondtab is unable to be updated. What is wrong? And what can be corrected?

This is the main.dart file:

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<ShareProgress>(
      create: (_) => ShareProgress(),
      child: MaterialApp(
        title: 'VideoTube',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          accentColor: Colors.red[400]
        ),
        debugShowCheckedModeBanner: false,
        home: App(),
      ),
    );
  }
}

This is the ShareProgress.dart file:

class ShareProgress extends ChangeNotifier {
  int _progress = 0;

  

  void setprogress(int newProgress) {
    _progress = newProgress;

    notifyListeners();
  }

  int get progress => _progress;
}

This is the main tab:

onReceiveProgress: (count, total) {
        var percentage = count / total * 100;

        

        ShareProgress().setprogress(count);
        setState(() {
          downloadMessage = 'downloadin.... ${percentage.floor()}%';
          

         // print(downloadMessage);
        });
        ShareProgress().setprogress(count);
      },

And this is second tab:

class _SecondTabState extends State<SecondTab> {
  @override
  Widget build(BuildContext context) {
    final progress = Provider.of<ShareProgress>(context);
    print(progress.progress);
    return Container(
      child: Text('second ${progress.progress}'),
    );
  }
}

Upvotes: 0

Views: 32

Answers (1)

Lab
Lab

Reputation: 1407

You have to use a Cunsumer to rebuild your widget:

class _SecondTabState extends State<SecondTab> {
  @override
  Widget build(BuildContext context) {
    return Consumer<KeyboardNotifier>(
            builder: (context, value, _) => Container(
               child: Text('second ${value.progress}'),
      ),
    );
  }
}

Upvotes: 1

Related Questions