0

I have a class RedditAPIService where i am putting all the items methods related to DRAW plugin for Reddit.

I created an object for the class in a Stateless widget. (below the class _RedditAuthState extends State portion)

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:

  @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)'."

What am i doing wrong?

NNY
  • 13
  • 2

1 Answers1

1

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. Try adding a return before the Scaffold widget and adding the child parameter to the callback function as below:

@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

This error is caused by the fact that the builder callback function is not returning anything, however a Widget is expected to be returned. 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. See here and here for more information.

(BuildContext) -> (BuildContext, Widget)

This error is caused by the fact that the builder callback function only has one parameter, however two parameters are expected. 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. See here and here for more information.

tnc1997
  • 1,832
  • 19
  • 20
  • Thank you so much! This fixed the error, now i am trying to understand this issue a little more. The initial code already had a 'return Scaffold' that would mean the function returned a widget correct? How does adding child to the arguments fix this? And what are we refering to by the child argument here. (If there are any docs you can refer me to that would be great.) Thank you so much. – NNY Jun 28 '20 at 07:01
  • You're welcome, happy to help! I've updated the answer to include an explanation of the error including links to the documentation. – tnc1997 Jun 28 '20 at 07:34