Debug in flutter AppBar

47 Views Asked by At

My code is :

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Bitcoin Weather',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: Text('Test')),
      ),
    );
  }
}

And this error appear :

The constructor being called isn't a const constructor. Try removing 'const' from the constructor invocation.

1

There are 1 best solutions below

0
Tofiq Samali On BEST ANSWER

The error message is telling you that the constructor for the MaterialApp widget is not a const constructor. This means that it cannot be used to create a constant widget.

To fix the error in your code, simply remove the const keyword from the MaterialApp constructor:

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Bitcoin Weather',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text('Test')
        ),
      ),
    );
  }
}

This will fix the error and your code will compile and run successfully.

Note: The Flutter linter will warn you about using non-const constructors, even if they are not causing any errors. This is because const constructors can improve the performance of your app. However, it is not always possible to use const constructors, so you can safely ignore these warnings if necessary.