[英]Flutter: Can't call Provider<T>.of(context) from a function that is defined into another file. ProviderNotFoundException
I'm new to flutter and I wish to organize my folders in order to write cleaner code.我是 flutter 的新手,我希望整理我的文件夹以编写更清晰的代码。 I'm trying to divide my flutter page on three parts:
我试图将我的 flutter 页面分为三个部分:
But when I call Provider.of(context) inside a functiton that is defined into login_builder.dart (out of the login_view.dart widget tree) it always throws ProviderNotFoundException但是当我在定义为 login_builder.dart 的函数中调用Provider.of(context)时(在 login_view.dart 小部件树之外),它总是抛出ProviderNotFoundException
// login_view.dart // 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 // 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 // login_state.dart
import 'package:flutter/material.dart';
class LoginState extends ChangeNotifier {
String _userName = "";
String get userName => _userName;
set userName(String userName) {
_userName = userName;
}
}
// Debug Console // 调试控制台
════════ 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
The context
you passed to login_builder is the same context that was passed to login_view, so it exists in a place of the widget tree above where you inserted your ChangeNotifierProvider
which is why you can't find it with Provider.of
.您传递给 login_builder 的
context
与传递给 login_view 的上下文相同,因此它存在于您插入ChangeNotifierProvider
的上方小部件树的位置,这就是为什么您无法使用Provider.of
找到它的原因。 In order to get this to work the way you want it to, you need to utilize a Builder
widget to gain a new BuildContext
that exists below the provider:为了让它按照你想要的方式工作,你需要利用一个
Builder
小部件来获得一个新的BuildContext
存在于提供者之下:
class Login extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<LoginState>(
create: (context) => LoginState(),
child: Builder(builder: buildLoginForm),
);
}
}
Now the context passed to buildLoginForm
will be the context for the Builder
(which exists below the provider) instead of for your Login
widget (which exists above the provider).现在传递给
buildLoginForm
的上下文将是Builder
的上下文(存在于提供者之下),而不是您的Login
小部件(存在于提供者之上)的上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.