Search code examples
flutterflutter-dependencies

"onPressed: () {}" doesn't work in flutter


 import 'package:flutter/material.dart';
 import 'package:audioplayers/audioplayers.dart';



  void main() {
    runApp(const XylophoneApp());
  }

 class XylophoneApp extends StatelessWidget {
   const XylophoneApp({super.key});

   @override
   Widget build(BuildContext context) {
     return const MaterialApp(
       home: Scaffold(
        body: SafeArea(
          child: Center(
            child: TextButton(onPressed: () {
              final player = Audiocache();
              player.play('note1.wav');

            },
              child: Text('Click Me'),
            ),
          ),
        ),
      ),
     );
  }
 }

when i click on run button it says "Error: Not a constant expression." and when i write a new onPressed on an another project its still the same error.


Solution

  • Remove const modifier on MaterialApp will do the fix.

    const MaterialApp -> MaterialApp.