Can't display const text in Flutter - flutter

I am using english-words package and I try to get random word and display it in Text but I got error. What I am doing wrong?
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final wordPair = WordPair.random();
return MaterialApp(
theme: ThemeData(primaryColor: Colors.purple[900]),
home: Scaffold(
appBar: AppBar(title: const Text('WordPair Generator')),
body: const Center(child: Text(wordPair.asPascalCase))
));
}
}
I get error at Text(wordPair.asPascalCase):
Evaluation of this constant expression throws an exception.dart(const_eval_throws_exception)

Remove the const keyword before the Center widget. you cant use a const widget if that widget has a none-constant data/variable at the build time.
Edit: More on const

Related

The argument type 'HomePageAppBar' can't be assigned to the parameter type 'ObstructingPreferredSizeWidget?'

The code was working well till when I tried Extract CupertinoNavigationBar to a widget.
May Anyone please give some information why this issue happening?
It gives me this error:
Error: The argument type 'HomePageAppBar' can't be assigned to the parameter type 'ObstructingPreferredSizeWidget?'.
package:flutter_todo_app/main.dart:27
'HomePageAppBar' is from 'package:flutter_todo_app/main.dart' ('lib/main.dart').
package:flutter_todo_app/main.dart:1
'ObstructingPreferredSizeWidget' is from 'package:flutter/src/cupertino/page_scaffold.dart' ('../../development/flutter/packages/flutter/lib/src/cupertino/page_scaffold.dart').
package:flutter/…/cupertino/page_scaffold.dart:1
navigationBar: HomePageAppBar(),
^
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'widgets/body.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return CupertinoApp(
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: HomePageAppBar(),
child: HomePageBody(),
);
}
}
class HomePageAppBar extends StatelessWidget {
const HomePageAppBar({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return CupertinoNavigationBar(
middle: const Text('app bar'),
);
}
}
wrap your HomePageAppBar() with CupertinoNavigationBar() like this :
CupertinoNavigationBar(
child: HomePageAppBar(),
)
Observe that CupertinoPageScaffold navigationBar accepts ObstructingPreferredSizeWidget but you are providing a Stateless Widget that's what the error says. Try implementing HomePageAppBar with ObstructingPreferredSizeWidget (refer to the below code). I ran your code on my machine it solved the error.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'widgets/body.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return CupertinoApp(
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: HomePageAppBar(),
child: HomePageBody(),
);
}
}
// <<<<<<<------ CHANGES ------>>>>>>
class HomePageAppBar extends StatelessWidget implements ObstructingPreferredSizeWidget {
const HomePageAppBar({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return const CupertinoNavigationBar(
middle: Text('app bar'),
);
}
#override
Size get preferredSize => const Size(double.infinity, 55);
#override
bool shouldFullyObstruct(BuildContext context) {
return false;
}
}

How do I run a different part of a folder in flutter (VS Codium)

I have made a new file in my views folder but whenever I turn on the emulator and run the code, it just says "Hello World".
Is there a way I can set the starting point of the project to be on this new file? Because it only seems to turn on the main.dart file.
This is the code that is in the views file called home_page.dart . It is supposed to just say "Hi" 10 times.
import 'package:flutter/material.dart';
import '../models/post.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<Post>? posts;
var isLoaded = false;
#override
void initState() {
super.initState();
//fetch data from API
getData();
}
getData() async {
// posts = await
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Posts'),
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return Container(
child: Text('Hi'),
);
},
)
);
}
}
in flutter the main.dart file is the first file
import 'package:flutter/material.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),//add your home page here
);
}
}

How to check if there is an update in Android OS using Flutter

I want to check if there is a software update in Android OS from Flutter side. Is there any package that helps me to do that?
Thanks for responses.
Yes you can use upgrader for this
import 'package:flutter/material.dart';
import 'package:upgrader/upgrader.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Upgrader Example',
home: Scaffold(
appBar: AppBar(title: Text('Upgrader Example')),
body: UpgradeAlert(
child: Center(child: Text('Checking...')),
)),
);
}
}

The constructor being called isn't a const constructor

I am getting an error for creating a Column Widget saying 'The constructor being called isn't a const constructor.' Having a tough time creating a Column Widget itself,
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(
debugShowCheckedModeBanner: false,
title: 'My First Flutter App',
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
),
home: const WelcomeScreen());
}
}
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const Scaffold(
body: Column(
children: <Widget>[
Text("Login"),
]
)
);
}}
Just remove const from Scaffold widget
Column is a non-const constructor
Column({
Key? key,
You can use const where everything is available as compile time constant. In this case, Column breaks the condition. You can use const before children, it is possible with Text for now
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: const <Widget>[
Text("Login"),
]),
);
}
}
I will encourage you to lean more about const, you can check this question and on guides.
The error is at the level of the scaffold widget.
Just remove the const before scaffold. Then add const keyword before the children of your column widget.
That is how your code is supposed to be
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(
debugShowCheckedModeBanner: false,
title: 'My First Flutter App',
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
),
home: const WelcomeScreen());
}
}
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: const <Widget>[
Text("Login"),
...
]
)
);
}}
Remove the const from the Scaffold widget and it would work fine. But there will be another warning telling that the Column widget does not have const.
To fix that you can either put const in front of <Widget> or put const in front of the children of columns

My Application is showing blank white canvas

#Problem#
Here is my code it is a simple app for practising provider package but when I run it a blank
canvas shows up.
#What I want from it#
Can anyone tell me the reason why nothing is showing up on screen? I have tried creating a fresh project and running it still didn't work.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => const MyApp();
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Data>(
create: (context) => Data(),
child: MaterialApp(
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const MyText(),
),
body: const Level1(),
),
),
);
}
}
class Level1 extends StatelessWidget {
const Level1({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const Level2();
}
}
class Level2 extends StatelessWidget {
const Level2({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Column(
children: const [
MyTextField(),
Level3(),
],
);
}
}
class Level3 extends StatelessWidget {
const Level3({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Text(Provider.of<Data>(context).data);
}
}
There is a tiny bug in your code... line 4
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
//void main() => const MyApp();
void main() => runApp(const MyApp()); // Update this line.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
...
In flutter land, the runApp function is responsible for inflating the root widget.
Update your main function to call the runApp function and pass in the root widget, MyApp in this case. That should solve the issue.