0

您好我正在尝试使用斩波器调用 POST api。但它给了我

“错误:在此 LoginPage 小部件上方找不到正确的提供程序”。

即使我已将提供程序作为祖先小部件添加到页面。

我究竟做错了什么?请帮忙。

 @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)),
                            ),
                          )
                      ),
);

}

4

2 回答 2

1

您收到错误是因为您没有将需要它的内容放在Provider上面。Login Page

你必须把Provider你正在使用的任何东西放在任何使用它的小部件之上。这个过程称为Lifting state up

要更正错误,请ProviderLoginWidget 中删除该小部件并将其包装在应用程序的根小部件中。

就像下面的代码:

void main() {
  runApp(
    Provider(
    create: (_) => ApiService.create(),
    dispose: (context, ApiService service) => service.client.dispose(),      
    child: MyApp(),
    ),
  );
}

阅读更多关于提升状态的信息。检查以下链接:

正确使用提供者

我希望这有帮助。

于 2020-05-06T10:06:14.237 回答
0

您可以使用下面的消费者来避免使用错误的上下文:

@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

于 2020-05-06T10:14:54.757 回答