Alex
Alex

Reputation: 827

Call an Object from another file

I have some code in main.dart file below. It will push to new Show() in show.dart file.

main.dart

import 'show.dart';
//.... 
Navigator.of(context).pop();
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => new Show()));
//...
 Future<void> _getSomething() async {
  }
//...

and the button I create on show.dart

                        //...                         
                        IconButton(
                            icon: Icon(
                              Icons.menu,
                              color: Colors.white,
                              size: 28,
                            ),
                            onPressed: () => print("back to main.dart and call _getSomething()"),
                          ),

Now, How can I back to main.dart and call _getSomething() from show.dart file ?

Upvotes: 0

Views: 151

Answers (1)

flarkmarup
flarkmarup

Reputation: 5439

You can pass a parameter (e.g., a bool) in the onPressed call that would then decide whether or not _getSomething() should be run when you're navigating to main.dart.

This article has a section called Give me some data back, man describing how to pass a value to the prior screen. Another benefit is that you're not adding additional pages to the stack if you chose to use Navigator.push() causing a weird flow when the user hits the back button.

Upvotes: 1

Related Questions