提供程序到另一个页面无法正常工作 [英] Provider to a different page not working as excpected

查看:36
本文介绍了提供程序到另一个页面无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新兴的开发人员,在通过上下文传递提供程序时遇到问题.如果我理解正确,则可以使用以下内容通过上下文传递提供程序: Provider.of< DataModel>(上下文);

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

错误是:在此PageMain窗口小部件上方找不到正确的提供程序"

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

我在窗口小部件树的顶部使用ChangeNotifierProvider,因此任何窗口小部件都可以访问DataModel类,目的是使它成为一种充当主题的单例.

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

然后,我在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);
  }

replaceWithMainPage是这样编写的util函数:

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

我正试图访问我在页面启动画面中注入的提供程序,如下所示: Provider.of< DataModel>(上下文);

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

DataModel代码:

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;

}

推荐答案

Flutter无法识别这两个路径,因为它们的大小写不同.修复导入,使它们引用相同的文件.

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.

这篇关于提供程序到另一个页面无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆