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:
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.')
),
);
}
}