简体   繁体   English

Stateful Widget 上的提供程序 - 参数类型问题

[英]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:该错误引用了两个不同的问题,我将在下面尝试解释:

Null -> Widget Null -> 小部件

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.有关更多信息,请参见此处此处

(BuildContext) -> (BuildContext, Widget) (BuildContext) -> (BuildContext, 小部件)

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.

相关问题 在有状态小部件中使用提供程序 - Using Provider in a Stateful Widget 将参数传递给有状态小部件 - Passing parameter to stateful widget 带参数的有状态小部件中的竞争条件 - Race condition in Stateful widget with parameter '放<widget> ' 不能分配给参数类型 'Widget'。 [供应商]</widget> - 'Set<Widget>' can't be assigned to the parameter type 'Widget'. [Provider] 如何将 Stateful Widget 中的 initState 替换为 provider 模式并将 stateless 替换为 stateful - How to replace initState in Stateful Widget with provider mode and replace stateless to stateful Provider (ChangeNotifier) 小部件内的有状态小部件未更新 - Stateful widget inside an Provider (ChangeNotifier) widget does not get updated Flutter 关于重用有状态小部件和更新 state 的问题 - Flutter issue on reusing stateful widget and updating state 在状态控件的状态下创建一个对象,该对象依赖提供者 - Create an object in stateful widget's state that has a dependency on provider 如何更改 Stateful Widget 中 Switch 的 state,从 Provider 检索数据? - How to change the state of a Switch in a Stateful Widget, retrieving data from a Provider? 不能在 showBotttomSheet 的有状态小部件内使用提供程序 - Provider can't be consumed inside stateful widget of showBotttomSheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM