Flutter 在flatter中使用提供者依赖
我正在尝试与Flatter中的提供商合作,但我不断遇到以下错误:Flutter 在flatter中使用提供者依赖,flutter,Flutter,我正在尝试与Flatter中的提供商合作,但我不断遇到以下错误: The following ProviderNotFoundException was thrown building Login(dirty, state: _LoginState#42a76): I/flutter ( 8335): Error: Could not find the correct Provider<CRUDModel> above this Login Widget I/flutter ( 83
The following ProviderNotFoundException was thrown building Login(dirty, state: _LoginState#42a76):
I/flutter ( 8335): Error: Could not find the correct Provider<CRUDModel> above this Login Widget
I/flutter ( 8335):
I/flutter ( 8335): To fix, please:
I/flutter ( 8335):
I/flutter ( 8335): * Ensure the Provider<CRUDModel> is an ancestor to this Login Widget
I/flutter ( 8335): * Provide types to Provider<CRUDModel>
I/flutter ( 8335): * Provide types to Consumer<CRUDModel>
I/flutter ( 8335): * Provide types to Provider.of<CRUDModel>()
I/flutter ( 8335): * Ensure the correct `context` is being used.
I/flutter ( 8335):
I/flutter ( 8335): If none of these solutions work, please file a bug at:
I/flutter ( 8335): https://github.com/rrousselGit/provider/issues
我不知道到底是什么问题。
我的应用程序中有一个启动屏幕,这使我无法在主类中添加提供商,因此我将其移动到我的滑动页面:该类包含登录和注册按钮这是我添加提供商的地方
这是我的滑杆课
class _SliderScreenState extends State<SliderScreen>{
int _current = 0;
List<SliderModel> sliderList = []...;
@override
Widget build(BuildContext context) {
final CarouselSlider coverScreenExample = CarouselSlider(... );
return
Scaffold(
backgroundColor: Color(0xffE5E5E5),
body:
Column(
children: <Widget>[
Expanded(
flex: 1,
child: coverScreenExample,
),
Padding(
padding: EdgeInsets.only(left: 34, right: 34,bottom: 100),
child: Row(
children: <Widget>[
Expanded(
flex:1,
child: CustomButton(
text:'Login',
textColor:accentColor,
fillColor: Colors.white,
height: 49,
function: (){
Navigator.pushReplacement(context,MaterialPageRoute(
builder: (context){
return **Provider<CRUDModel>.value(
value: CRUDModel(),
child:** Login() ,
);
}
));
// Navigator.pushNamed(context, '/login');
}
)
),
SizedBox(width: 61,),
Expanded(
flex: 1,
child: CustomButton(
text:'Register',
textColor: Colors.white,
height: 49,
fillColor:accentColor,
function: (){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=>Registeration()));
})
),
],
) ,) ,
],
)
);
}
List<T> map<T>(List list, Function handler) {
List<T> result = [];
for (var i = 0; i < list.length; i++) {
result.add(handler(i, list[i]));
}
return result;
}
}
问题是,每当我点击登录按钮时,我都会在屏幕和日志上看到上面显示的错误消息,但是如果我热加载错误页面,我会看到我的登录屏幕
这是我的CRUDModel课程
class CRUDModel extends ChangeNotifier{
API _api = locator<API>();
Future signUp( BuildContext context, SignUpModel data) async{
var result ;
_api.path= (BASE_URL+"signup");
Response v = await _api.postDetails(data.toJson());
final responseJson = json.decode(v.body);
final int statusCode =responseJson.statusCode;
if(statusCode < 200 || statusCode > 400 ||json == null){
result = throw new Exception("SignUP:: Error while fetching data");
}
else if(statusCode == 200){
result = SignUpModel.fromJson(json.decode(responseJson));
Navigator.push(context, MaterialPageRoute(builder: (context)=> Login()));
}
return result;
}
Future login( BuildContext context, LoginModel data) async{
var result ;
_api.path = BASE_URL+"login";
Response v = await _api.postDetails(data.toJson());
final responseJson = json.decode(v.body);
final int statusCode =responseJson.statusCode;
if(statusCode < 200 || statusCode > 400 ||json == null){
result = throw new Exception("SignUP:: Error while fetching data");
}
else if(statusCode == 200){
result = SignUpModel.fromJson(json.decode(responseJson));
Navigator.push(context, MaterialPageRoute(builder: (context)=> HomePage()));
}
return result;
}
}
我做错了什么?不使用此选项:
Navigator.pushReplacement(context,MaterialPageRoute(
builder: (context){
return Provider<CRUDModel>.value(
value: CRUDModel(),
child: Login() ,
);
}
));
如果你想在你的登录中使用你的提供者,你可以使用Consumer,或者你可以这样称呼它
final crudModelProvider = Provider.of<CrudModel>(context);
我已经这样做了,但仍然会遇到同样的错误。我必须清理我的代码,但我再也看不到那个错误了。谢谢Selim Kundakıoğlu
final crudModelProvider = Provider.of<CrudModel>(context);