Title: Allowing 'context' to travel to different dart files
Tags: flutter;dart
Question: Currently have a Navigator.push(context, route) but the context portion is returning an error, After trying to debug I found that the issue was because i was calling a function vs setting the home directly to the widget tree. But now i'm not sure how I can get the context to communicate to the build? Not sure if this was well explained but hopefully looking at the code clarifies my issue.
passing 'context' in the mainview() function, get a runtime error.
class _MyAppmain extends State<MyApp&gt; {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
//theme data
primaryColor: Color(0xffA3E4D7),
accentColor: Color(0xffECF0F1),
fontFamily: 'Montserrat'),
home: mainView());//this is where the function in the other file is called
second file:
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
Navigator.push(context, NewEventTransition());//the issue
},
mini: true,
),
this is a very small chunk of the file but I think it should be enough.
Wanting to be able to get context across the two files through the function but I get an error instead.


Here is another answer: Navigator is a widget just like Scaffold but much more near the top of the widget tree.
calling Navigator.of(context) will get you the navigator widget in the context's widget tree.
[when the fab is pressed, there is definitely a Navigator in the widget tree. maybe each context can only find widgets with lower depth in the widget tree]
a fix would be to use a named route builder instead of a function call:
class _MyAppmain extends State<MyApp&gt; {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
//theme data
primaryColor: Color(0xffA3E4D7),
accentColor: Color(0xffECF0F1),
fontFamily: 'Montserrat'),
initialRoute: '/',
routes: {
'/' : (context) =&gt; mainView(context),
},
);
Comment for this answer: appreciate the response!

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:09/12/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<flutter><dart>', 'Title': "Allowing 'context' to travel to different dart files", 'ViewCount': 824, 'id': 57898825, 'user_AboutMe': '', 'user_AccountId': 14585486.0, 'user_DisplayName': 'Marko', 'user_Reputation': 435.0, 'user_Views': 92.0, 'user_id': 10534453.0}}