首页 > 解决方案 > Flutter WEB,布局模板。pop 返回 void 但期望 Bool

问题描述

我正在尝试跟随本教程。进度在这里: https ://github.com/vinceyoumans/flutweb001/tree/menu001


AssetNotFoundException: the_basics|lib/services/navigation_service.ddc.js
Error compiling dartdevc module:the_basics|lib/services/navigation_service.ddc.js

packages/the_basics/services/navigation_service.dart:12:38: Error: This expression has type 'void' and
can't be used.
    return navigatorKey.currentState.pop();

代码在:/lib/services/navigation_services.dart


import 'package:flutter/material.dart';

class NavigationService {
  final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();

  Future<dynamic> navigateTo(String routeName) {
    return navigatorKey.currentState.pushNamed(routeName);
  }

  bool goBack() {
    return navigatorKey.currentState.pop();
  }
}

我得到的错误:

无法从方法“goBack”返回类型为“void”的值,因为该行的返回类型为“bool”.dart(return_of_invalid_type)”

navigatorKey.currentState.pop();

因此,当它需要一个布尔值时,它似乎返回了一个 void。关于如何解决这个问题的任何想法?

标签: webflutterdartnavigation

解决方案


更改返回类型时没有任何影响。您可以简单地从

navigatorKey.currentState.pop();

并更新您的函数以返回 void。我还没有看到它在我的项目中造成任何问题。


推荐阅读