Search code examples
flutterscaffold

Getting error in FloatingActionButton (Scaffold) Flutter Ubuntu


I am new to flutter started just right now, was following one tutorial, even though I copied exactly same code it's giving me error. Here is my code.

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
    home: Scaffold(
      
      appBar: AppBar(
        title: Text('App Bar'),
        centerTitle: true
      ), //Appbar
      
      body: Center(
        child: Text('Hello!'),
      ), //Body
      
      floatingActionButton: FloatingActionButton(
        child: Text('Click')
      ), //FloatingActionButton

    ), //Scaffold
));

And here is the ERROR!

lib/main.dart:15:49: Error: Required named parameter 'onPressed' must be
provided.
      floatingActionButton: FloatingActionButton(
                                                ^
/home/hackytech/snap/flutter/common/flutter/packages/flutter/lib/src/material/fl
oating_action_button.dart:100:9: Context: Found this candidate, but the
arguments don't match.
  const FloatingActionButton({

Please can anyone explain me what's the problem and it will be excellent if I get a tip for learning flutter or any source as I am new to this framework

Thank you :)


Solution

  • onPressed method is required for FloatingActionButton

    Therefore, while using FloatingActionButton you need to call onPressed.

    VoidCallback? onPressed

    floatingActionButton: FloatingActionButton(
      onPressed: () {}, //you can also pass null here
      child: Text("text"),
    ),
    

    While you like to have Text widget on floatingActionButton: you can simply use Text(or any other widget) widget there.

    floatingActionButton: Text("text"),