![](/img/trans.png)
[英]Flutter Provider -Adding Proxy Provider that has dependency on Stream Provider
[英]Using Provider dependency in flutter
我正在尝试与颤振中的提供者合作,但我不断收到此错误:
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() ,
);
}
));
你能试试这个吗:
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) {
return ChangeNotifierProvider(
create: (context) => CrudModel(),
child: Login(),
);
}
));
如果你想在你的登录中使用你的提供者,你可以使用Consumer
或者你可以像这样调用它
final crudModelProvider = Provider.of<CrudModel>(context);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.