fmmalta / no_context_navigation_package

A Flutter package built to navigate between screens without using context (BuildContext)
https://pub.dev/packages/no_context_navigation
MIT License
9 stars 1 forks source link

Error: This expression has type 'void' and can't be used. navigationKey.currentState.pop<T>(result); #2

Closed sallaben closed 4 years ago

sallaben commented 4 years ago
Compiler message:
../flutter/.pub-cache/hosted/pub.dartlang.org/no_context_navigation-1.0.1/lib/src/navigation_service.dart:40:57: Error: This expression has type 'void' and can't be used.
  bool goBack({T result}) => navigationKey.currentState.pop<T>(result); 

Compiling with flutter sdk channel master for macos. Any ideas?

fmmalta commented 4 years ago

Can you describe it more? I'm current using but got no errors until now...

fmmalta commented 4 years ago

Try specifying the goBack() type, like this:

navService.goBack<void>();

fmmalta commented 4 years ago

Fixed in new version. Closing

kamleshwebtech commented 4 years ago
bool goBack({Object result}) => navigationKey.currentState.pop<bool>(result);

"navigationKey.currentState.pop(result);"

returns an error

"A value of type 'void' can't be returned from method 'goBack' because it has a return type of 'bool'."

I am using latest version - no_context_navigation 1.0.2

https://pub.dev/packages/no_context_navigation

Please help, how to fix this issue.

fmmalta commented 4 years ago
bool goBack({Object result}) => navigationKey.currentState.pop<bool>(result);

"navigationKey.currentState.pop(result);"

returns an error

"A value of type 'void' can't be returned from method 'goBack' because it has a return type of 'bool'."

I am using latest version - no_context_navigation 1.0.2

https://pub.dev/packages/no_context_navigation

Please help, how to fix this issue.

Are you sure you're in the latest version? Because I've updated this function to this:

void goBack({T result}) => navigationKey.currentState.pop<T>(result);

Also, make sure you have had set NavigationKey in MaterialApp, navigationKey parameter.

kamleshwebtech commented 4 years ago

Thanks @fmmalta . Your last given solution worked.

kamleshwebtech commented 4 years ago

Could you please suggest me how can we pass multiple parameters in pushNamed function:

navService.pushNamed(ListofarticlesScreen.routeName, args: {'search', 'python'});

not receiving 'search' and 'python' params in 'ListofarticlesScreen.dart' constructor. Please suggest.

fmmalta commented 4 years ago

Could you please suggest me how can we pass multiple parameters in pushNamed function:

navService.pushNamed(ListofarticlesScreen.routeName, args: {'search', 'python'});

not receiving 'search' and 'python' params in 'ListofarticlesScreen.dart' constructor. Please suggest.

Create a model/helper object with all parameters you need, like:

class MyModel {
  final String myParam1;
  final int myParam2;
  final double myParam3;
  final bool myParam4;

  const MyModel({this.myParam1, this.myParam2, this.myParam3, this.myParam4});
}

Fill this parameters before you pass in the args parameters.

onPressed: () => navService.pushNamed('/my-screen, 
args: MyModel(myParam1: ..., 
myParam2: ..., 
myParam3: ..., 
myParam4: ..., 
myParam5: ....
 ),
);

In your router.dart file, an example, use this: case '/my-screen': return MyScreen(myModel: settings.arguments);

On MyScreen, you will use MyModel as an object in your constructor, like this:

class MyClass {
  final MyModel myModel;

  const MyClass({Key key, this.myModel}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _myString = myModel.myParam1; ///Example getting the parameters from MyModel;
    return ...
  }
}

It's better to encapsulating your arguments/parameters instead of doing like you were doing. Make things much easier.

kamleshwebtech commented 4 years ago

Thank you so much, you solved my another issue. 👍