0

I have created an AppBar on a separate page. So, that I can include it wherever required. This App Bar contains a popup menu. I want if the user selects items from the menu then it will redirect to that page.

AppBar.dart

import 'package:flutter/material.dart';
import 'package:fnapp/home_screen.dart';

class Choice {
  const Choice({this.title, this.icon});
  final String title;
  final IconData icon;
}

const List<Choice> choices = const <Choice>[
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
];


class BaseAppBar extends StatelessWidget with PreferredSizeWidget {
  final Color backgroundColor = Colors.red;
   @override
  Widget build(BuildContext context) {

    return AppBar(
      backgroundColor: Colors.transparent,
        elevation: 0.0,
        leading: BackButton(
          color: Colors.black87,
          onPressed: (){//  onPressed: () => Navigator.of(context).pop(),
            print('back pressed');
          },
        ), 
        actions: <Widget>[ 

        PopupMenuButton<Choice>(
              icon: Icon(
                Icons.more_vert,
                color: Colors.black,
              ),
              onSelected: _select,
              itemBuilder: (BuildContext context) {
              return choices.skip(0).map((Choice choice) {
                return PopupMenuItem<Choice>(
                  value: choice,
                  child: Text(choice.title)
                  );
                }).toList();
              },
        ),
        ],

    );

  }

  @override
 // Size get preferredSize => new Size.fromHeight(appBar.preferredSize.height);
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
void _select(Choice choice) {
    if(choice.title == 'Bus'){
      print('Bus');
    }else if(choice.title == 'Car'){
     // print('Car');
     _navigateToHome();
    }

  }

_navigateToHome() Plan is using this Function to basically redirect it on another page.

void _navigateToHome(){
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        builder: (BuildContext context) => HomeScreen()
      )
    );
  }

HomeScreen() page is added in AppBar.dart file.

Profile.dart

import 'dart:math';
import 'package:fnapp/appbar.dart';
import 'package:flutter/material.dart';
import 'package:fnapp/util/data.dart';
import 'package:flutter/services.dart';
import 'package:fnapp/home_screen.dart';

class OthersProfile extends StatefulWidget {
  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<OthersProfile> {
  @override
    void _navigateToHome(){
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        builder: (BuildContext context) => HomeScreen()
      )
    );
  }
  int currentIndex = 0;
@override
  Widget build(BuildContext context) {

    return Scaffold(

        appBar: BaseAppBar(),

      body:----some long codes here----
),
},
},

I tried to using _navigateToHome() function to inside/outside the Scaffold/Widget Build even in both files (AppBar and Profile). But it is giving me below error.

Compiler message: lib/appbar.dart:66:6: Error: Method not found: '_navigateToHome'. _navigateToHome();

Compiler message: lib/appbar.dart:88:18: Error: Getter not found: 'context'. Navigator.of(context).pushReplacement(

How can I fix this issue?

Edit

After suggestions, I have removed underscore from function. But it is still giving me an error.

I removed _ from both files. I removed from AppBar under the _Select function. But it is not working giving the same error. Also, I tried to move Select function from AppBar to Profile but it is also giving error.

I think similar issue was raised in GitHub by the user. https://github.com/flutter/flutter/issues/21728

halfer
  • 19,824
  • 17
  • 99
  • 186
Roxx
  • 3,738
  • 20
  • 92
  • 155
  • 1
    try with normal method, not with private..I had same issue earlier and solved through this approach. – Sanket Vekariya Apr 18 '20 at 05:30
  • Thanks for comment Sanket. Any examples for the same. I am quite new in Flutter. Leaning now. – Roxx Apr 18 '20 at 05:32
  • Remove the initial _ on the name of the method. The _ indicates it's private and therefore can only be used within the same file. – Juan V Apr 18 '20 at 05:34
  • 1
    It would be complex to give an example right now..but you can give at least a try to remove that underscore from all the method and implementation... If it is not working, I will surely come with the example. :) – Sanket Vekariya Apr 18 '20 at 05:38
  • Thanks for the comments guys. Just one help. Do i need to remove _ from both files or just from AppBar.dart. I removed from AppBar under the _Select function. But it is not working giving same error. Also i tried to move Select function from AppBar to Profile but it is also giving error. – Roxx Apr 18 '20 at 06:11
  • @JuanV I tried multiple times but it is getting failed. If i move select() function from Appbar to Profile it is giving error related to select function. If i move navigatetohome function from profile to Appbar it is giving error for context. However, i have removed all the _ (underscores) – Roxx Apr 18 '20 at 06:22
  • consider post full code of `BaseAppBar`. We need to see how you declare `_navigateToHome` in `BaseAppBar`. – John Joe Apr 18 '20 at 09:42
  • @JohnJoe Codes are already given in the question. Its under AppBar.dat. – Roxx Apr 18 '20 at 10:01
  • put them together instead of seperate in your question. – John Joe Apr 18 '20 at 10:18
  • I am planning to use AppBar as a separate file. So, i can add them wherever required. If i need to make changes in Appbar then i might need to update all files. Having a single file will help me in that situation. – Roxx Apr 18 '20 at 10:34

1 Answers1

0

The error is clear on the logs: Error: Method not found: '_navigateToHome'. _navigateToHome(); - the method you're trying to call isn't accessible.

If you'd like to call the _navigateToHome() function from a different class, you can follow this similar Stack Overflow answer where the function is passed as a constructor to be accessible.

Omatt
  • 8,564
  • 2
  • 42
  • 144