繁体   English   中英

Flutter:无法调用提供商<t> .of(context) 来自定义在另一个文件中的 function。 ProviderNotFoundException</t>

[英]Flutter: Can't call Provider<T>.of(context) from a function that is defined into another file. ProviderNotFoundException

我是 flutter 的新手,我希望整理我的文件夹以编写更清晰的代码。 我试图将我的 flutter 页面分为三个部分:

  1. login_view.dart (此视图将呈现为登录视图,并将调用定义在 login_builder.dart 中的函数来构建其每个小部件)
  2. login_builder.dart (包含由 login_view.dart 调用的 function 定义以构建小部件)
  3. login_state.dart (用于state管理)

但是当我在定义为 login_builder.dart 的函数中调用Provider.of(context)时(在 login_view.dart 小部件树之外),它总是抛出ProviderNotFoundException

// login_view.dart

import 'package:discover/ui/views/login/login_builder.dart';
import 'package:discover/ui/views/login/login_state.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Login extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<LoginState>(
      create: (context) => LoginState(),
      child: buildLoginForm(context), 
    );
  }
}

// login_builder.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:discover/ui/views/login/login_state.dart';

Widget buildLoginForm(BuildContext context) {
  var loginState = Provider.of<LoginState>(context);
  return Form(
    child: Column(
      children: <Widget>[
        TextFormField(
          onChanged: (value) => loginState.userName = value,
        )
      ],
    ),
  );
}

// login_state.dart

import 'package:flutter/material.dart';

class LoginState extends ChangeNotifier {
  String _userName = "";

  String get userName => _userName;

  set userName(String userName) {
    _userName = userName;
  }
}

// 调试控制台

════════ Exception caught by widgets library ═══════════════════════════════════
The following ProviderNotFoundException was thrown building Login(dirty):
Error: Could not find the correct Provider<LoginState> above this Login Widget

To fix, please:

  * Ensure the Provider<LoginState> is an ancestor to this Login Widget
  * Provide types to Provider<LoginState>
  * Provide types to Consumer<LoginState>
  * Provide types to Provider.of<LoginState>()
  * Ensure the correct `context` is being used.

If none of these solutions work, please file a bug at:
https://github.com/rrousselGit/provider/issues

The relevant error-causing widget was
    Login
When the exception was thrown, this was the stack
Provider.of
buildLoginForm
Login.build
StatelessElement.build
ComponentElement.performRebuild

您传递给 login_builder 的context与传递给 login_view 的上下文相同,因此它存在于您插入ChangeNotifierProvider的上方小部件树的位置,这就是为什么您无法使用Provider.of找到它的原因。 为了让它按照你想要的方式工作,你需要利用一个Builder小部件来获得一个新的BuildContext存在于提供者之下:

class Login extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<LoginState>(
      create: (context) => LoginState(),
      child: Builder(builder: buildLoginForm), 
    );
  }
}

现在传递给buildLoginForm的上下文将是Builder的上下文(存在于提供者之下),而不是您的Login小部件(存在于提供者之上)的上下文。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM