Provider to a different page not working as excpected

我怕爱的太早我们不能终老 提交于 2021-01-21 06:04:24

问题


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;

}

回答1:


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()
    )
  );
}



回答2:


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.



来源:https://stackoverflow.com/questions/65790413/provider-to-a-different-page-not-working-as-excpected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!