void main() {
runApp(
const MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('I am Rich'),
),
),
),
);
}
this is my code and there is showing "the constructor called is not a constructor"
Error message-
lib/main.dart:8:17: Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
appBar: AppBar(
^^^^^^
tried adding 'const' before scaffold
I am sorry for the silly question. I am just a beginner. can't find the solution anywhere else.
To be a bit more precise: AppBar doesn't have a const constructor, so you can't use the const keyword. However, Text does have a const constructor, so you can (should) use const for that one.
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('I am Rich'),
),
),
),
);
}
Just remove const
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('I am Rich'),
),
),
),
);
}
Remove the 'const' before MaterialApp. It should work fine.
the correct code should be
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('I am Rich'),
),
),
),
);
}
Related
How do I fix it const issue on appBar ?
While you are adding title:Text(..) AppBar is not const anymore. Therefore, the parent MaterialApp cant be const.
You can do
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("this is te"),
),
),
);
}
Your code will have diagnostic-messages on error:
const_with_non_const
// correct
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Scaffold(),
),
);
}
// fail
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Scaffold(
appBar: AppBar(),
),
),
);
}
While Text('AppBar Demo') is a const, it is preferred to add const before it.
// not preferred
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AppBar Demo'),
),
),
),
);
}
// preferred
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('AppBar Demo'),
),
),
),
);
}
Today I have a button that calls the method _launchURL. But I'd like to call this method right after the app opens, without the need to press a button.
import 'package:flutter/material.dart';
import 'package:flutter_custom_tabs/flutter_custom_tabs.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Custom Tabs Example',
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.light,
),
darkTheme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.dark,
),
home: Builder(
builder: (_context) => Scaffold(
appBar: AppBar(
title: const Text('Flutter Custom Tabs Example'),
brightness: Brightness.dark,
),
body: Center(
child: TextButton(
onPressed: () => _launchURL(_context),
child: Text(
'Open Page',
style: TextStyle(fontSize: 17),
),
),
),
),
),
);
}
Future<void> _launchURL(BuildContext context) async {
final theme = Theme.of(context);
try {
await launch(
'https://www.google.com/',
customTabsOption: CustomTabsOption(
toolbarColor: theme.primaryColor,
enableDefaultShare: true,
enableUrlBarHiding: true,
showPageTitle: true,
animation: CustomTabsSystemAnimation.slideIn(),
extraCustomTabs: const <String>[
// ref. https://play.google.com/store/apps/details?id=org.mozilla.firefox
'org.mozilla.firefox',
// ref. https://play.google.com/store/apps/details?id=com.microsoft.emmx
'com.microsoft.emmx',
],
),
safariVCOption: SafariViewControllerOption(
preferredBarTintColor: theme.primaryColor,
preferredControlTintColor: Colors.white,
barCollapsingEnabled: true,
entersReaderIfAvailable: false,
dismissButtonStyle: SafariViewControllerDismissButtonStyle.close,
),
);
} catch (e) {
// An exception is thrown if browser app is not installed on Android device.
debugPrint(e.toString());
}
}
}
Widget build(BuildContext context) {
_launchURL(context); // <= here
return MaterialApp(
title: 'Flutter Custom Tabs Example',
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.light,
),
.
.
.
.
Or you can add it in initstate, but your method needs a BuildContext to work.
I have an array that has an image URL and normal string as its content. I want to display an image or Text widget based on which value is being read. I have used this to make that work:
Widget build(BuildContext context) {
final usrMap = {"tom", 'tom.png', "taommy"};
return MaterialApp(
title: 'Flutter Tutorial',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Text Widget Tutorial'),
),
body: Column(
children: [
for( var prop in usrMap){
prop.contains(".com")? Text(prop) : Text("not .png")
},
]
),
),
);
}
I am getting this error
The element type 'Set' can't be assigned to the list type 'Widget'
How do I fix this?
use like this.
Widget build(BuildContext context) {
final usrMap = {
"tom",
'tom.png',
"taommy",
"sheikh.com",
};
return MaterialApp(
title: 'Flutter Tutorial',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Text Widget Tutorial'),
),
body: Column(children: [
...usrMap.map((e) {
if (e.contains(".com"))
return Text("prop");
else
return Text("not .png");
}).toList(),
]),
),
);
}
i am new to flutter and as i try creating a new project, here is the error i am getting:
The method 'Text' isn't defined for the type '_MyHomePageState'.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: Text('Material App Bar'),
),
body: Center(
child: Container(
child: Text('Hello World'),
),
),
),
);
}
}
Try to use hot restart, your code works fine.
The main flutter folder is distorted by some extensions installed, the solution is just to delete and download it again and still in the same previous directory, the you can install it back from the terminal
when returning MaterialApp flutter shows error as follow "error: 1 required argument(s) expected, but 0 found. (not_enough_required_arguments at [demo] lib/main.dart:9)"Here is the screenshot
const _categoryName = "Cake";
const _categoryIcon = Icons.cake;
const _categoryColor = Colors.green;
import 'package:solution_02_category_widget/category.dart';
void main() {
runApp(UnitConverterApp());
}
class UnitConverterApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Unit Converter',
home: Scaffold(
backgroundColor: Colors.green[100],
body: Center(
child: Category(
name: _categoryName,
color: _categoryColor,
iconLocation: _categoryIcon,
),
),
),
);
}
}
You should wrap the title argument under AppBar Widget. You can in turn wrap the appBar argument inside the Scaffold. The scaffold is like an empty page that gives your app the white background. Below is a sample code implementation:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
// debugShowMaterialGrid: true,
theme: ThemeData(
brightness: Brightness .light,
accentColor: Colors.deepPurple,
primarySwatch: Colors.deepOrange),
home: Scaffold(
appBar: AppBar(
title: Text('Some text'),
),
),
);
}
}
Your code is working fine without error if i remove the Category() widget. The below code is working fine:
import 'package:flutter/material.dart';
const _categoryName = "Cake";
const _categoryIcon = Icons.cake;
const _categoryColor = Colors.green;
void main() {
runApp(UnitConverterApp());
}
class UnitConverterApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Unit Converter',
home: Scaffold(
backgroundColor: Colors.green[100],
body: Center(
child: Container(),
),
),
);
}
}
Maybe the problem is in the Category() widget so if you post it i can check it for you.