类型'Future <Null>'不是类型'Widget'的子类型问题

时间:2020-10-20 08:13:43

标签: flutter dart

我的代码有问题,我才刚刚开始学习颤振,为此我需要解决方案

错误:“ Future”类型不是“ Widget”类型的子类型

这是我的代码

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

如果您听不懂,可以要求解释:谢谢

2 个答案:

答案 0 :(得分:1)

存在多个问题。首先,routeToGPS返回null。您尝试将其用作小部件。它应该返回这样的小部件:

     Future<Widget> routeToGPS(int index) 

但是您想在那里更改当前页面。它不会打开任何小部件。您需要使用onPress函数返回一个按钮小部件,该按钮小部件可将您导航到所需页面。

答案 1 :(得分:1)

我现在找到了解决方案,谢谢@Akif的建议

只需检查initState中的条件,并对其进行一些延迟,以便将服务器的响应添加到变量

@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);
  }