1

I'm a new flutter developer, and I have a problem with passing the provider through context. If I understood correctly, the provider can be passed by context using: Provider.of<DataModel>(context);

The error is: "Could not find the correct Provider above this PageMain Widget"

I am using the ChangeNotifierProvider, at the top of my widget tree, so any widget could access the DataModel class, with the intent of making it a sort of a singleton that acts as subject.

            return ChangeNotifierProvider<DataModel>(
              create: (context) => DataModel(),
              builder: (context, dataModel){
                  return PageSplash();
              }

Then, I have this function in the PageSplash(); Which is being called after an authentication of the user. In it I pass the context with the details of the user.

  void _showNextPage(User user) {
    PageShower.replaceWithMainPage(context, user, AppData.instance.user);
  }

The replaceWithMainPage is a util function that is written like this:

static void replaceWithMainPage(BuildContext context, User user, RMUser receetMeUser) {
    if (Platform.isAndroid) {
      Navigator.of(context).pushReplacement(
        MaterialPageRoute(
          builder: (context) => PageMain(
            firebaseUser: user,
            receetMeUser: receetMeUser,
          ),
        ),
      );
    } else {
      Navigator.of(context).pushReplacement(
        CupertinoPageRoute(
          builder: (context) => PageMain(
            firebaseUser: user,
            receetMeUser: receetMeUser,
          ),
        ),
      );
    }
  }

I'm trying to access the provider that I injected in the page splash like this: Provider.of<DataModel>(context);

DataModel code:

import 'package:flutter/material.dart';

class DataModel with ChangeNotifier{
  bool _isLoading = true;
  bool _isSearching = true;

  String _version = "";
  String _buildNumber = "";

  set buildNumber(String value){
    _buildNumber = value;
    notifyListeners();
  }

  get buildNumber => _buildNumber;

  set version(String value){
    _version = value;
    notifyListeners();
  }

  get version => _version;

  set isLoading(bool value){
    _isLoading = value;
    notifyListeners();
  }

  get isLoading => _isLoading;

  set isSearching(bool value){
    _isSearching = value;
    notifyListeners();
  }

  get isSearching => _isSearching;

}
  • 2
    Post your `DataModel` content – dm_tr Jan 19 '21 at 11:21
  • Does this answer your question? [Could not find the correct provider above this widget](https://stackoverflow.com/questions/57124258/could-not-find-the-correct-provider-above-this-widget) – dm_tr Jan 19 '21 at 11:47
  • @dm_tr to my understanding, Instead of separating that part of the code to a different standalone widget tree, I can use the Builder method, so that it gives me the context of the parent which should result with the exact same behavior. – Amit Ritter Jan 19 '21 at 12:10

2 Answers2

0

I take a different approach to providers. I always put a multiprovider code block before the app in the main method. See my main below and tell me if it works for you. It works perfectly for me. I have added your DataModel in the multiprovider code block.

void main() {
  runApp(
    MultiProvider(providers: [
      BlocProvider<EditPlusTableBloc>(
        create: (_) => EditPlusTableBloc(),
      ),
      BlocProvider<DataModel>(
        create: (_) => DataModel(),
      ),
    ],
    child : ExampleApp()
    )
  );
}
0

Flutter doesn't recognize the two paths as the same since they differ in upper vs lower case. Fix your imports so that they reference the same file.

Mr. ZeroOne
  • 904
  • 6
  • 20
  • I didn't quite understand what you meant. Are you saying that the class DataModel isn't contained in a file that has the same name? – Amit Ritter Jan 19 '21 at 12:00
  • Yes. Fix your import. For example: This import 'models/DataModel.dart' and this This import 'Models/DataModel.dart' is different. one of these will not work. – Mr. ZeroOne Jan 20 '21 at 04:44