skip to Main Content

there is error in override function body error

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Row(
          children: [
            Text("Flutter"),
            Text(
              "News",
              style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
            )
          ],
          

        ),
        centerTitle: true,
        
      ),
      body: Container(),
    );
  }
}

class CategoryTile extends StatelessWidget {
  final image, categoryName;
  CategoryTile({super.key, this.categoryName, this.image})


  // ignore: empty_constructor_bodies
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack( children: [
        Image.asset(image,
        width: 120,
        height: 60,
        )
      ],),
    );
  }
}

a function body must be provided error

i dont know how to solve this & so I can get some help.

2

Answers


  1. You are missing a ; after your constructor :

    class CategoryTile extends StatelessWidget {
      final image, categoryName;
      CategoryTile({super.key, this.categoryName, this.image});
    
    
      // ignore: empty_constructor_bodies
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Stack( children: [
            Image.asset(image,
            width: 120,
            height: 60,
            )
          ],),
        );
      }
    }
    

    It should be better like this

    Login or Signup to reply.
  2. When you define a constructor, you need to provide a function body for it.

    Here is the updated code:

    class CategoryTile extends StatelessWidget {
      final String image;
      final String categoryName;
    
      CategoryTile({
        Key? key,
        required this.categoryName,
        required this.image,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Stack(
            children: [
              Image.asset(
                image,
                width: 120,
                height: 60,
              )
            ],
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search