【问题标题】:The argument type 'FutureBuilder<NewsModel>' can't be assigned to the parameter type 'Future<Object?>?'参数类型“FutureBuilder<NewsModel>”不能分配给参数类型“Future<Object?>?”
【发布时间】:2022-01-11 01:38:31
【问题描述】:

我的代码中遇到了这个错误:

参数类型“FutureBuilder”不能分配给参数类型“Future?”

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());
          },
        ),
      ),
    );
  }
}

帮助我解决这个错误。谢谢。

【问题讨论】:

    标签: flutter flutter-dependencies flutter-futurebuilder


    【解决方案1】:

    FutureBuilderfuture 接受Future。所以_newsModel 应该是Future,而不是FutureBuilder

    【讨论】:

    • 我试过了,但对我不起作用。你能从你的最后检查它是否工作吗?
    • 对不起,我写错了命令。你说的对。它对我有用。赞赏。感谢您的帮助。
    猜你喜欢
    • 2021-08-27
    • 2022-01-05
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2021-10-10
    • 2021-06-08
    • 2020-12-03
    相关资源
    最近更新 更多