-1

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 :)

Hackytech
  • 317
  • 2
  • 11
  • please read the error message. You must provide `onPressed` property to `FloatingActionButton`, You're only providing `child` parameter of the class. `onPressed` is mandatory field – HasilT Dec 23 '21 at 08:45
  • Well sorry bro @HasilT AS I clearly mentioned I am new to this, I don't know what that error says. Thanks for informing. I didn't knew that its important field. In that Youtube channel he ran that code without that parameter and it worked perfectly. – Hackytech Dec 23 '21 at 09:37

3 Answers3

3

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"),

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
1

pass on pressed property

floatingActionButton: FloatingActionButton(
        child: Text('Click'),
        onPressed: () {
              print("click");
            }
      ), //FloatingActionButton

Jahidul Islam
  • 11,435
  • 3
  • 17
  • 38
0

floatingActionButton requires onPressed attached. and Need to be within Scaffold Widget.

SO

  floatingActionButton: FloatingActionButton(
     onPressed: () { //anything you need to do after tap  },
    tooltip: 'Shopping Bag',
    child: const Icon(Icons.shopping_basket),
  ),
infomasud
  • 2,263
  • 1
  • 18
  • 12