我正试图与供应商合作,但我不断收到此错误:
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;
}
}
我在做什么错了?
答案 0 :(得分:1)
代替此:
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);