In home.dart
I call a navigation function inside an Elevated Button
onPressed: (){
Navigator.of(context).push(
MaterialPageRoute(
builder: (context)=>{
return const GameScreen();
}
));
},
Which will navigate to GameScreen.
game_screen.dart
import 'package:flutter/material.dart';
class GameScreen extends StatelessWidget {
const GameScreen({ Key? key }) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(backgroundColor: Colors.white,);
}
}
However I get the red squiggly lines under the navigation function saying
The return type "Set<GameScreen>" isn't a "Widget", as required by the closure's context.
Why is this giving me an error and how do I fix this?
CodePudding user response:
Well you're returning a Set<Widget>
The problem is how you're using the arrow operator =>
, you cannot use {}
and the return
keyword with the arrow operator.
The =>
operator already works like a return
. And in your case the {}
is being interpreted like a Set literal.
Try this instead:
MaterialPageRoute(
builder: (context) => const GameScreen();
));
Or this:
MaterialPageRoute(
builder: (context) {
return const GameScreen();
}
));
CodePudding user response:
I think you are confused with typescript, the method syntax is not () => {...}
, this would return a Set
of ...
Do this instead () {...}
(runs ...
):
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return const GameScreen();
}
));
},
Rr this () => ...
(returns ...
):
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const GameScreen(),
));
},