【问题标题】:type 'Future<List<dynamic>>' is not a subtype of type 'Future<List<FoodModel>>''Future<List<dynamic>>' 类型不是 'Future<List<FoodModel>>' 类型的子类型
【发布时间】:2020-09-20 15:22:42
【问题描述】:

所以我试图让我的 foodmodel 数据显示在我的屏幕上,我不知道这段代码有什么问题,它一定是关于 await async 但我无法弄清楚。它只说Future&lt;List&lt;dynamic&gt;&gt; 类型不是Future&lt;List&lt;FoodModel&gt;&gt; 类型的子类型。我还是新手。有没有办法通过这段代码使用异步等待来获取数据?

class FoodPage extends StatefulWidget {
  @override
  _FoodPageState createState() => _FoodPageState();
}
class _FoodPageState extends State<FoodPage> {
  Future<List<FoodModel>>  foods;
  @override
  initState()  {
    super.initState();
    setState(() {
      var db= DatabaseHelperFood();
      foods=db.getFoods();
    });
  }
  GridView  food_data (List<FoodModel> foods)
  {
    return  GridView.builder(

      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
      itemCount: foods.length,
      itemBuilder: (_, int position)
      {
        return  Card(

          child:Container(
              decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(FoodModel.map(foods[position]).alamat),
                    fit: BoxFit.cover,
                  )),
              child: Container(
                  margin: EdgeInsets.fromLTRB(
                      0, 152, 0, 0),
                  child: Column(
                    children: <Widget>[
                      Container(
                          width: 500,
                          height: 25,
                          child: RaisedButton(
                            onPressed: () {

                            },
                            child: Text('Beli : $FoodModel.map(_foods[position]).harga',
                                textAlign: TextAlign
                                    .center,
                                style: TextStyle(
                                    color: Colors.white)),
                            color: Colors.black,
                          ))
                    ],
                  )
              )
          ),);
      },
    );
  }
  foodlist(){
    return Expanded(
        child:FutureBuilder(
            future:foods,
            builder:(context,datas)
            {
              if(datas.hasData)
              {
                return food_data(datas.data);
              }
              else if(null== datas.data || datas.data.length==0)
              {
                return Text("No data");
              }
              return CircularProgressIndicator();
            }
        )
    );
  }
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            backgroundColor: Colors.grey[900],
            leading: Icon(Icons.menu),
            actions: <Widget>[
              Padding(
                  padding: EdgeInsets.all(5),
                  child: Row(children: <Widget>[
                    Container(

                      child:Text("GoFlix",style:TextStyle(fontSize: 30)),
                      margin:EdgeInsets.only(right:90),
                    ),
                    Container(
                        child: Icon(Icons.shopping_cart, size: 30),
                        margin: EdgeInsets.only(top: 5)),
                    Container(
                      margin: EdgeInsets.only(top: 5),
                      width: 40,
                      height: 40,
                      decoration: BoxDecoration(
                          color: Colors.redAccent[800],
                          borderRadius: BorderRadius.circular(10)),
                      child: Center(child: Text("0")),
                    )
                  ]))
            ]),
        body:Container(
            color:Colors.grey[800],
            child: Column(
                children: <Widget>[
                  Image.asset('Asset/Movieabanner.jpg'),
                  foodlist(),

                ])
        )
    );
  }
}

【问题讨论】:

  • 代码中的错误在哪里?它在这里:future:foods 吗?

标签: flutter mobile


【解决方案1】:

如果您的 Future 值为 List&lt;FoodModel&gt;,您需要按如下方式构建您的 FutureBuilder:

FutureBuilder<List<FoodModel>>(
  future: foods,
  builder: (context, datas) {

  }
)

由于 FutureBuilder 处理异步等待的事情,而不是在 initState 方法中调用 db.getFoods(),您可以在 FutureBuilder 中执行如下操作:

FutureBuilder<List<FoodModel>>(
  future: db.getFoods(),
  builder: (context, datas) {

  }
)

但是你在其他一些地方使用食物,所以你可能需要更多的重构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2020-02-28
    • 2021-08-13
    • 2021-10-20
    • 1970-01-01
    • 2022-01-23
    • 2019-12-31
    相关资源
    最近更新 更多