IrishGringo
IrishGringo

Reputation: 4054

Flutter WEB, Layout Templates. pop returning void but expects Bool

I am trying to following along with this tutorial. Progress is here: 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();

The code is in: /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();
  }
}

The error I am getting:

A value of type 'void' can't be returned from method 'goBack' because it has a return type of 'bool'.dart(return_of_invalid_type) from this line"

navigatorKey.currentState.pop();

So it appears a void is being returned when it expects a Bool. any thoughts on how to fix this?

Upvotes: 3

Views: 1061

Answers (3)

offgridauthor
offgridauthor

Reputation: 36

May be necessary to add a null check (!) also:

  void goBack() {
     navigatorKey.currentState!.pop();
  }

Upvotes: 0

Mawuli Dickson
Mawuli Dickson

Reputation: 16

Also, you can change the return type of the function to void or simply remove it.

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

Upvotes: 0

Filled Stacks
Filled Stacks

Reputation: 4356

There's no implications when changing the return type. You can simply remove the return from

navigatorKey.currentState.pop();

and update your function to return void. I haven't seen it causing any problems in my projects.

Upvotes: 3

Related Questions