Why Flutter complains about 2 body parameters? - flutter

In the tutorial we see two body parameters: https://docs.flutter.dev/get-started/codelab
Like that:
body: const Center(
22
- child: Text('Hello World'),
23
+ body: Center(
24
+ child: Text(wordPair.asPascalCase),
But on the dartpad.dev I have an error:
The argument for the named parameter 'body' was already specified.
I have this code:
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
final wordPair = WordPair.random();
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(title: Text('Hello there')),
body: const Center(
child: Text('The word is'),
),
body: Center(child: Text(wordPair)),
),
);
}
}

The first snippet shows the changes made in the starter code. They don't coexist
If you look at the left you see the minus and plus sign.

Related

I am trying to build the main.dart file but getting this error

import 'package:expense_management/widgets/user_transaction.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter App',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter App'),
),
body: Column(
children: <Widget>[
Container(
width: double.infinity,
child: const Card(
color: Colors.orange,
elevation: 5,
child: Text('chart'),
),
),
userTransaction()
],
));
}
}
error--------------------
The following Unimplemented Error was thrown building Keyed
Subtree-[GlobalKey#87727]: UnimplementedError
The relevant error-causing widget was: Scaffold
Scaffold:file:///C:/Users/abhargaw/Downloads/development/projects/expense_management/lib/main.dart:21:1
2

This method 'Scaffold' isn't defined for the type 'MyApp'

I have reinstalled the flutter and Android Studio to my device and after that Scaffold widget is not working. It says "This method 'Scaffold' isn't defined for the type 'MyApp' "
add wrap Scaffold with MaterialApp widget
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: const Text('Welcome to Flutter'),
),
body: const Center(
child: Text('Hello World'),
),
),
);
}
}

The constructor being called isn't a const constructor?

I am brand new to flutter and programming in general -so please bear with me-, so I am taking a course on udemy and I stumbled with this error (The constructor being called isn't a const constructor)
This is the code :
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar:AppBar(
title: Text('My First App'),
),
body: Text('This is my default text'),
),
);
}
}
Try below code hope its help to you. remove const keyword
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: Text('This is my default text'),
),
);
}
}
Your result screen->

Flutter using a changenotifier with named routes

I've followed some online tutorials and managed to implement ChangeNotifier when the app has a single route however none of these explain how to implement this when the app has more than one route (screen) which is rather the point!
I made an attempt to figure this out myself but when the app runs in the emulator I get a blank screen.
/* main.dart */
import 'dart:collection'; // used in test.dart
import 'package:flutter/foundation.dart'; // used in test.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Person extends ChangeNotifier {
Person({this.firstName});
String firstName = '';
void updateName(String n) {
this.firstName = n;
notifyListeners();
}
}
void main() {
runApp(
Provider(
create: (_) => Person(firstName: 'Mark'),
child: MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => HomeRoute(),
'/second': (context) => SecondRoute(),
'/third': (context) => ThirdRoute(),
},
),
)
);
}
class HomeRoute extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(Provider.of<Person>(context).firstName),
),
body: Center(
child: new IconButton(
icon: new Icon(Icons.favorite, color: Colors.redAccent),
iconSize: 70.0,
onPressed: () {
Navigator.of(context).pushNamed("/second");
}
),
),
);
}
}
class SecondRoute extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Second Page'),
),
body: Center(
child: Text(Provider.of<Person>(context).firstName),
),
);
}
}
class ThirdRoute extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Third Page'),
),
body: Center(
child: Text('Third Route'),
),
);
}
}
The project builds and runs with no error messages which has stumped me.
Any thoughts?
Code developed in FlutLab

what is the error ? and how can i solve it?

I am learning flutter. i have made one app name chat x(title). the code is below
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Chat X'),
),
body: Card(child: Column(children: <Widget>[
Text('hey! this one is new');
],),),
),
);
}
}
when ever i want to debug this code i get this error
Expected to find ']'.
Remove ; from Text('hey! this one is new'); or you can place a , if you want to have more widgets inside Column
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Chat X'),
),
body: Card(child: Column(children: <Widget>[
Text('hey! this one is new'),
Text('hey! this one is second new'),
],),),
),
);
}
}
You shoud not have the ; after the Text widget. Like this:
Text('hey! this one is new')