Vince
Vince

Reputation: 43

type 'Future<List<dynamic>>' is not a subtype of type 'Future<List<FoodModel>>'

So I'm trying to get my foodmodel data to show up on my screen, I don't know what is wrong with this code, it must be something about await async but I can't figure it out. It only says type Future<List<dynamic>> is not a subtype of type Future<List<FoodModel>>. I'm still new to flutter. Is there anyway to get data using async await from this code?

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

                ])
        )
    );
  }
}

Upvotes: 0

Views: 269

Answers (1)

If your Future value is List<FoodModel> you need to build your FutureBuilder as follows:

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

  }
)

Since FutureBuilder handles async await things, instead of calling db.getFoods() inside your initState method, you can do it inside FutureBuilder as follows:

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

  }
)

But you are using foods in some other places, so you might need a little more refactoring.

Upvotes: 2

Related Questions