I am facing this error in my code:
The argument type 'FutureBuilder' can't be assigned to the parameter type 'Future<Object?>?'
class _HomePageState extends State<HomePage> {
late FutureBuilder<NewsModel> _newsModel;
@override
void initState() {
_newsModel = API_Manager().getNews() as FutureBuilder<NewsModel>;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(child: Text('News App')),
),
body: Container(
child: FutureBuilder(
future: _newsModel,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(itemBuilder: (context, index) {
return Container(
height: 100,
color: Colors.red,
);
});
} else
return Center(child: CircularProgressIndicator());
},
),
),
);
}
}
Help me through this to get rid of this error. Thanks.
CodePudding user response:
FutureBuilder
's future
accept a Future
. So _newsModel
should be a Future
, instead of a FutureBuilder
.