Error: Could not find the correct Provider<ApiService> above this LoginPage Widget
Error: Could not find the correct Provider<ApiService> above this LoginPage Widget
您好,我正在尝试使用 chopper 呼叫 POST api。但它给了我
"Error: Could not find the correct Provider above this LoginPage Widget"。
甚至我已经将提供程序作为祖先小部件添加到页面。
我做错了什么?请帮忙。
@override
Widget build(BuildContext context) {
return Provider(
create: (_) => ApiService.create(),
dispose: (context, ApiService service) => service.client.dispose(),
child: Container(
width: double.infinity,
child: ButtonTheme(
height: 36.0,
child: RaisedButton(
onPressed: () async {
//Navigator.pushReplacementNamed(context, "/myjourney");
print("Arpit: ${_emailController.text + " " +_passwordController.text}");
generateMd5(_passwordController.text);
String email = _emailController.text;
String password = generateMd5(_passwordController.text);
final response = await Provider.of<ApiService>(context)
.doLogin("d1d2fe0514f7d5c748c0e7e085b36f74",email,password,"App");
print(response.body);
},
color: Colors.white,
disabledColor: Colors.white,
shape: BeveledRectangleBorder(),
textColor: Colors.indigo,
padding: EdgeInsets.all(8.0),
splashColor: Color(0xFF6c60e4),
child: Text("SIGN IN", style: TextStyle(
fontFamily: "Montserrat",
fontSize: 15.0,
color: Colors.indigo,fontWeight: FontWeight.bold)),
),
)
),
);
}
您收到错误是因为您没有将 Provider
放在需要它的 Login Page
之上。
您必须将您正在使用的任何 Provider
放在使用它的任何 Widget 之上。这个过程叫做Lifting state up
.
要更正错误,请从 Login
小部件中删除 Provider
小部件并将其包装在应用程序的根小部件中。
喜欢下面的代码:
void main() {
runApp(
Provider(
create: (_) => ApiService.create(),
dispose: (context, ApiService service) => service.client.dispose(),
child: MyApp(),
),
);
}
阅读更多关于提升状态的信息。检查下面的 link:
希望对您有所帮助。
您可以使用下面的 Consumer 来避免使用错误的上下文:
@override
Widget build(BuildContext context) {
return Provider(
create: (_) => Foo(),
child: Consumer<Foo>(
builder: (_, foo, __) => Text(foo.value),
},
);
}
像这样:
@override
Widget build(BuildContext context) {
return Provider<ApiService>(
create: (_) => ApiService.create(),
dispose: (context, ApiService service) => service.client.dispose(),
child: Consumer<ApiService>(
builder: (_, apiService, __) => Container(
width: double.infinity,
child: ButtonTheme(
height: 36.0,
child: RaisedButton(
onPressed: () async {
//Navigator.pushReplacementNamed(context, "/myjourney");
print("Arpit: ${_emailController.text + " " +_passwordController.text}");
generateMd5(_passwordController.text);
String email = _emailController.text;
String password = generateMd5(_passwordController.text);
final response = await apiService.doLogin("d1d2fe0514f7d5c748c0e7e085b36f74",email,password,"App");
print(response.body);
},
color: Colors.white,
disabledColor: Colors.white,
shape: BeveledRectangleBorder(),
textColor: Colors.indigo,
padding: EdgeInsets.all(8.0),
splashColor: Color(0xFF6c60e4),
child: Text("SIGN IN", style: TextStyle(
fontFamily: "Montserrat",
fontSize: 15.0,
color: Colors.indigo,fontWeight: FontWeight.bold)),
),
)
),
);
在此处查看消费者文档:
https://pub.dev/documentation/provider/latest/provider/Consumer-class.html
您好,我正在尝试使用 chopper 呼叫 POST api。但它给了我
"Error: Could not find the correct Provider above this LoginPage Widget"。
甚至我已经将提供程序作为祖先小部件添加到页面。
我做错了什么?请帮忙。
@override
Widget build(BuildContext context) {
return Provider(
create: (_) => ApiService.create(),
dispose: (context, ApiService service) => service.client.dispose(),
child: Container(
width: double.infinity,
child: ButtonTheme(
height: 36.0,
child: RaisedButton(
onPressed: () async {
//Navigator.pushReplacementNamed(context, "/myjourney");
print("Arpit: ${_emailController.text + " " +_passwordController.text}");
generateMd5(_passwordController.text);
String email = _emailController.text;
String password = generateMd5(_passwordController.text);
final response = await Provider.of<ApiService>(context)
.doLogin("d1d2fe0514f7d5c748c0e7e085b36f74",email,password,"App");
print(response.body);
},
color: Colors.white,
disabledColor: Colors.white,
shape: BeveledRectangleBorder(),
textColor: Colors.indigo,
padding: EdgeInsets.all(8.0),
splashColor: Color(0xFF6c60e4),
child: Text("SIGN IN", style: TextStyle(
fontFamily: "Montserrat",
fontSize: 15.0,
color: Colors.indigo,fontWeight: FontWeight.bold)),
),
)
),
);
}
您收到错误是因为您没有将 Provider
放在需要它的 Login Page
之上。
您必须将您正在使用的任何 Provider
放在使用它的任何 Widget 之上。这个过程叫做Lifting state up
.
要更正错误,请从 Login
小部件中删除 Provider
小部件并将其包装在应用程序的根小部件中。
喜欢下面的代码:
void main() {
runApp(
Provider(
create: (_) => ApiService.create(),
dispose: (context, ApiService service) => service.client.dispose(),
child: MyApp(),
),
);
}
阅读更多关于提升状态的信息。检查下面的 link:
希望对您有所帮助。
您可以使用下面的 Consumer 来避免使用错误的上下文:
@override
Widget build(BuildContext context) {
return Provider(
create: (_) => Foo(),
child: Consumer<Foo>(
builder: (_, foo, __) => Text(foo.value),
},
);
}
像这样:
@override
Widget build(BuildContext context) {
return Provider<ApiService>(
create: (_) => ApiService.create(),
dispose: (context, ApiService service) => service.client.dispose(),
child: Consumer<ApiService>(
builder: (_, apiService, __) => Container(
width: double.infinity,
child: ButtonTheme(
height: 36.0,
child: RaisedButton(
onPressed: () async {
//Navigator.pushReplacementNamed(context, "/myjourney");
print("Arpit: ${_emailController.text + " " +_passwordController.text}");
generateMd5(_passwordController.text);
String email = _emailController.text;
String password = generateMd5(_passwordController.text);
final response = await apiService.doLogin("d1d2fe0514f7d5c748c0e7e085b36f74",email,password,"App");
print(response.body);
},
color: Colors.white,
disabledColor: Colors.white,
shape: BeveledRectangleBorder(),
textColor: Colors.indigo,
padding: EdgeInsets.all(8.0),
splashColor: Color(0xFF6c60e4),
child: Text("SIGN IN", style: TextStyle(
fontFamily: "Montserrat",
fontSize: 15.0,
color: Colors.indigo,fontWeight: FontWeight.bold)),
),
)
),
);
在此处查看消费者文档: https://pub.dev/documentation/provider/latest/provider/Consumer-class.html