[英]Provider on Stateful Widget - issue with parameter type
I have a class RedditAPIService where i am putting all the items methods related to DRAW plugin for Reddit.我有一个 class RedditAPIService,我在其中放置了与 Reddit 的 DRAW 插件相关的所有项目方法。
I created an object for the class in a Stateless widget.我在无状态小部件中为 class 创建了 object。 (below the class _RedditAuthState extends State portion)
(在 class _RedditAuthState 下方扩展 State 部分)
RedditAPIService reddit = RedditAPIService();
RedditAPIService reddit = RedditAPIService();
I need this reddit object to be available on multiple Widgets downstream so i wanted to use Provider to expose the object:我需要这个 reddit object 在下游的多个小部件上可用,所以我想使用 Provider 来公开 object:
@override
Widget build(BuildContext context) {
return Provider<RedditAPIService>(
create: (_) => RedditAPIService(),
builder: (context) {
Scaffold(
appBar: GlobalAppBar(
appbarTitle: 'Authorize ReadStories',
),
body: SafeArea(
child: Center(
child: haveRedditAuthCode
? CircularProgressIndicator()
: WebviewScaffold(
url: reddit.getAuthURL(),
hidden: true,
// initialChild: Center(child: Text('data')),
),
)),
);
},
);
}
}
I am currently getting the error:我目前收到错误:
"error: The argument type 'Null Function(BuildContext)' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget)'."
“错误:参数类型'Null Function(BuildContext)'不能分配给参数类型'Widget Function(BuildContext,Widget)'。”
What am i doing wrong?我究竟做错了什么?
I believe that you are getting this error because you are not returning anything from the builder
callback function, hence the Null
in the error message.我相信您收到此错误是因为您没有从
builder
回调 function 中返回任何内容,因此错误消息中的Null
。 Try adding a return
before the Scaffold
widget and adding the child
parameter to the callback function as below:尝试在
Scaffold
小部件之前添加return
并将child
参数添加到回调 function 如下:
@override
Widget build(BuildContext context) {
return Provider<RedditAPIService>(
create: (_) => RedditAPIService(),
builder: (context, child) {
return Scaffold(
appBar: GlobalAppBar(
appbarTitle: 'Authorize ReadStories',
),
body: SafeArea(
child: Center(
child: haveRedditAuthCode
? CircularProgressIndicator()
: WebviewScaffold(
url: reddit.getAuthURL(),
hidden: true,
// initialChild: Center(child: Text('data')),
),
)),
);
},
);
}
The error is referencing two separate issues which I will attempt to explain below:该错误引用了两个不同的问题,我将在下面尝试解释:
This error is caused by the fact that the builder callback function is not returning anything, however a Widget
is expected to be returned.此错误是由于构建器回调 function 未返回任何内容而导致的,但预计会返回一个
Widget
。 This Widget
is what gets displayed in the User Interface of the application and is much the same as returning a Widget
from the overridden build
method.这个
Widget
显示在应用程序的用户界面中,与从覆盖的build
方法返回一个Widget
非常相似。 See here and here for more information.有关更多信息,请参见此处和 此处。
This error is caused by the fact that the builder callback function only has one parameter, however two parameters are expected.此错误是由于构建器回调 function 只有一个参数,但需要两个参数引起的。 The second parameter is a child
Widget
, which can be used if you have a particularly large child Widget
in the tree that does not need to be recreated each time that the state changes.第二个参数是子
Widget
,如果你在树中有一个特别大的子Widget
并且不需要每次 state 更改时重新创建,则可以使用它。 See here and here for more information.有关更多信息,请参见此处和此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.