Home > Net >  How can I fix my issue with onPressed in Flutter?
How can I fix my issue with onPressed in Flutter?

Time:02-19

I am new to Flutter programming and I am having an issue navigating to a new page and getting the onPressed to work correctly. I am using the snippet of code below and I have seen other tutorial videos do the same exact thing, however when I use the snippet everything from "() { Navigator.push( ..... ); }" gets underlined in red with the error:

Invalid constant value.dart(invalid_constant)

I'm not sure how to fix this error and any help would be greatly appreciated, thank you.

const ListTile(
              title: Text('About Me'),
              subtitle: Text('Account Information'),
              trailing: IconButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const About()),
                  );
                },
                icon: Icon(Icons.keyboard_arrow_right_rounded),
              ),
            ),

CodePudding user response:

The error is related to ListTile onTap property.

The Widget ListTile already have a definition of onTap, so the onPressed property inside IconButton will never be triggered.

Try this:

ListTile(
  title: const Text('About Me'),
  subtitle: const Text('Account Information'),
  onTap: () {
    Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => const About()),
    );
  },
  trailing: const Icon(Icons.keyboard_arrow_right_rounded,),

CodePudding user response:

Try removing the keyword const near ListTile. If the constructor of About is not const, remove it also from the push method.

CodePudding user response:

As of now, dart function doesn't support constant literal. You are trying to make ListTile a constant constructor but onPressed takes a function as an argument that can not be constant or final. Either remove const from your ListTile or create another static function and then pass it to onPressed.

You can check from the below link.

https://github.com/dart-lang/language/issues/1048

You can also use NavigatorState for navigation.

   class MyApp2 extends StatelessWidget {
    
      static final navigatorStateKey = GlobalKey<NavigatorState>();
      const MyApp2({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
           key: navigatorStateKey,
           home: ListView(
             children: List.generate(20, (index) =>
                 const ListTile(
                  title: Text('About Me'),
                  subtitle: Text('Account Information'),
                  trailing: IconButton(
                    onPressed:  onPressed,
                   icon: Icon(Icons.keyboard_arrow_right_rounded),
                  ),
                ),
            ),
          ),
        );
    
      }
      static void onPressed() {
        MyApp2.navigatorStateKey.currentState.push( 
      MaterialPageRoute(builder: (context) => const About()));
       }
      }
  • Related