Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter non-const constructor error within AppBar when trying to use text inside material app

Hello I have a quick question, what am I doing wrong here? I am trying to make an AppBar within a Scaffold however when I try to use Text it doesn’t seem to work and says to add a Const, however when I do it doesn’t solve the issue.

Sorry if there is information out there already for this, I just don’t know the specific terms to look up to solve this issue. I know you can put the AppBar in the void main() however I am following a tutorial and would like to do it similarly to that.

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('My First App'),
          ),
          body: Text('This is the body of text.')
      ),
    );
  }
}

This is the error that is outputted:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

12:25: Error: Cannot invoke a non-‘const’ constructor where a const
expression is expected. Try using a constructor or factory that is
‘const’.
appBar: const AppBar(
^^^^^^

>Solution :

Just remove const before Material app


import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('My First App'),
          ),
          body: Text('This is the body of text.')
      ),
    );
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading