The argument type 'FutureBuilder<NewsModel>' can't be assigned to the parameter type 'Future<Object?>?'

Issue

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.

Solution

FutureBuilder‘s future accept a Future. So _newsModel should be a Future, instead of a FutureBuilder.

enter image description here

Answered By – 聂超群

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.