0

On app homepage I set up Model2 which make API call for data. User can then navigate to other page (Navigator.push). But I want make API call from Model2 when user press back (_onBackPress()) so can refresh data on homepage. When I make call, it give error:

Unhandled Exception: Error: Could not find the correct Provider above this Consumer Widget

If I add Consumer to Page2 it give error:

Error: Could not find the correct Provider above this Consumer Widget

StatefulWidget in homepage:

@override
  Widget build(BuildContext context) {
return ChangeNotifierProxyProvider<Model1, Model2>(
initialBuilder: (_) => Model2(),
  builder: (_, model1, model2) => model2
    ..string = model1.string,
),
  child: Consumer<Model2>(
    builder: (context, model2, _) =>

...
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => SecondRoute(context: context)),

In Page2:

  final context;

  Page2State({Key key, this.context});
...

//    Consumer<Model2>(
//    builder: (context, model2, _) =>
    ...
          onWillPop: () => _onBackPress(context),
...

Future<void> _onBackPress(context) async {

final model2 = Provider.of<Model2>(context, listen: false);

  return showDialog<void>(
    context: context,
    barrierDismissible: false, 
    builder: (BuildContext context) {
      return 


    Provider.value(value: model2, child:


AlertDialog(
        title: Text('Back'),
        content: SingleChildScrollView(
          child: ListBody(
            children: <Widget>[
              Text('Go back'),
            ],
          ),
        ),
        actions: <Widget>[
          FlatButton(
            child: Text('OK'),
            onPressed: () async {
await model2.getData();

              Navigator.of(context).pop();
            },
          ),
        ],
),
      );
    },
  );
}
FlutterFirebase
  • 2,163
  • 6
  • 28
  • 60
  • Again, you need to add your providers to the `navigator.push` builder. – Rémi Rousselet Aug 20 '19 at 21:05
  • @RémiRousselet Thanks!! But I am already push context in `Navigator.push(context, MaterialPageRoute(builder: (context) => SecondRoute()),`? – FlutterFirebase Aug 20 '19 at 21:10
  • It doesn't matter. you still need to include the necessary providers – Rémi Rousselet Aug 20 '19 at 22:37
  • @RémiRousselet Issue is `Model2` is only initialised for some users. Other user group not have `Model2`. But if I call final `model2 = Provider.of(context, listen: false);` for user who not have `Model2` initialise, it give error – FlutterFirebase Aug 21 '19 at 14:58
  • @RémiRousselet Still have same error when pass in context with: `MaterialPageRoute(builder: (context) => SecondRoute(context: context))`. I have update code above – FlutterFirebase Aug 21 '19 at 16:20

1 Answers1

1

Perhaps a bit hacky, but overriding the dispose() method on your model/provider classes seems to work.


E.g., if you have a viewmodel offered as a provider to your view:

class View extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      lazy: false,
      create: (context) => ViewModel(),
      builder: (context, child) {
        return Scaffold(
...
}
class ViewModel with ChangeNotifier {
...
  @override
  // ignore: must_call_super
  void dispose() {}
}

This will stop the model from being automatically disposed by the provider mechanism. Of course now you have a useless dispose() method, so you might need to implement some other method that calls super.dispose() to free resources when needed:

  void myDispose() {
    super.dispose();
  }
nelsonspbr
  • 162
  • 8