Issue
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?
Solution
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.
Answered By – tnc1997
Answer Checked By – Clifford M. (FlutterFixes Volunteer)