title: How to access a function from a different dart file in Flutter?
body: <p>This is my HomePage.dart Codes. Here I Want to access the _pushSaved() function into the 'onpressed' property from a different file named &quot;randomwords.dart&quot; file (code attached for review). It seems I need to declare the _pushSaved() somewhere but do not have any idea as I am new to dart and flutter. I Will be thankful if somebody could help. Thanks in advance.</p>
  <pre><code>
  
  class HomePage extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              title: Text(&quot;StartUp Name Generator&quot;),
              actions: [
                IconButton(icon: Icon(Icons.list), **onPressed: _pushSaved**)
          ],
        ),
        body: RandomWords(),
      );
    }
  }
  </code></pre>
  <p>The _pushSaved() function code in the randomwords.dart file needs to be accessed in the HomePage.dart as object...</p>
  <pre><code>class RandomWords extends StatefulWidget {
    @override
    _RandomWordsState createState() =&gt; _RandomWordsState();
  }
  
  class _RandomWordsState extends State&lt;RandomWords&gt; {
    final _suggestions = &lt;WordPair&gt;[];
    final _saved = Set&lt;WordPair&gt;();
    final _biggerFont = TextStyle(fontSize: 20.0, color: Colors.pink);
    @override
    Widget build(BuildContext context) {
      return ListView.builder(
        itemBuilder: (context, i) {
          if (i.isOdd) return Divider();
  
          final index = i ~/ 2;
          if (index &gt;= _suggestions.length) {
            _suggestions.addAll(generateWordPairs().take(10));
          }
          return _buildRow(_suggestions[index]);
        },
      );
    }
  
    Widget _buildRow(WordPair pair) {
      final alreadySaved = _saved.contains(pair);
      return ListTile(
        title: Text(
          pair.asPascalCase,
          style: _biggerFont,
        ),
        trailing: Icon(
          alreadySaved ? Icons.favorite : Icons.favorite_border,
          color: alreadySaved ? Colors.red : null,
        ),
        onTap: () {
          setState(() {
            if (alreadySaved) {
              _saved.remove(pair);
            } else {
              _saved.add(pair);
            }
          });
        },
      );
    }
  
    void **_pushSaved()** {
      Navigator.of(context).push(
        MaterialPageRoute&lt;void&gt;(
          builder: (BuildContext context) {
            final tiles = _saved.map(
              (WordPair pair) {
                return ListTile(
                  title: Text(
                    pair.asPascalCase,
                    style: _biggerFont,
                  ),
                );
              },
            );
  
            final divided =
                ListTile.divideTiles(tiles: tiles, context: context).toList();
  
            return Scaffold(
              appBar: AppBar(
                title: Text(&quot;Saved Suggestions&quot;),
              ),
              body: ListView(
                children: divided,
              ),
            );
          },
        ),
      );
    }
  }
  </code></pre>