type 'Future<Null>' is not a subtype of type 'Widget' Problem

Viewed 2679

I have problem with my code And I'm just start to learn about flutter I need solution for this

Error : type 'Future' is not a subtype of type 'Widget'

Here my code

  Widget build(BuildContext context) {
    return orderStatus ? Style1().showProgress() : buildContent();
  }

  Widget buildContent() => ListView.builder(
        padding: EdgeInsets.all(10.0),
        itemCount: orderModels.length,
        // ignore: unrelated_type_equality_checks
        itemBuilder: (context, index) => riders[index] == "NONE"
            ? processFoods[index] == "UserOrder"
                ? orderCards(globalModels, index)
                : Container()
            : riders[index] == "$nameRider"
                ? processFoods[index] == "Cooking"
                    ? routeToGPS(index)
                    : Container()
                : Container(),
      );


Future<Null> routeToGPS(int index) async {
    await Future.delayed(Duration(seconds: 3));
    MaterialPageRoute route = MaterialPageRoute(
      builder: (context) => NavGps(
        orderModel: orderModels[index],
      ),
    );
    Navigator.push(context, route);
  }
}

If you don't understand something you can ask for explain thank for help :D

2 Answers

There is more than one problem. First, routeToGPS returns null. And you tried to use it as a widget. It should return a widget like this:

     Future<Widget> routeToGPS(int index) 

But you wanted to change the current page in there. It won't turn any widget. You need to return a button widget with the onPress function which navigates you to the expected page.

I found the solution now Thank you @Akif for suggestion

Just check condition in initState and put some delay on it for add response from server to variable

@override
  void initState() {
    findOrder();
    checkJobs();
    super.initState();
  }

  Timer checkJobs() {
    return Timer(Duration(seconds: 2), () {
    int count = orderModels.length;
    for (var i = 0; i < count; i++) {
      if (orderModels[i].rider == nameRider &&
          orderModels[i].process == "Cooking") {
        routeToGPS(i);
      }
    }
  });
  }

  @override
  Widget build(BuildContext context) {
    return orderStatus ? Style1().showProgress() : buildContent();
  }

  Widget buildContent() => ListView.builder(
        padding: EdgeInsets.all(10.0),
        itemCount: orderModels.length,
        // ignore: unrelated_type_equality_checks
        itemBuilder: (context, index) => riders[index] == "NONE"
            ? processFoods[index] == "UserOrder"
                ? orderCards(globalModels, index)
                : Container()
            // : riders[index] == "$nameRider"
            //     ? processFoods[index] == "Cooking" ? Container() : Container()
            : Container(),
      );
      
    Future<Widget> routeToGPS(int index) {
    MaterialPageRoute route = MaterialPageRoute(
      builder: (context) => NavGps(
        orderModel: orderModels[index],
      ),
    );
    Navigator.push(context, route);
  }
Related