Using Visual Studio Code, my emulator is completely blank.
There is plenty of code but it is stating that there are no errors but nothing is showing up on emulator. Thanks of the help.
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: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
)
);
}
}
class FirstPage extends StatelessWidget {
const FirstPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('First Page'),
),
body: const Center(
child: Text('This is the first page'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const SecondPage(data: 'Hello from the first page!'),
)
);
}
)
);
}}
class SecondPage extends StatelessWidget {
final String data;
const SecondPage({super.key, required this.data});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Second Page'),
),
body: Center(
child: Text(data),
),
);
}
}
What could be reason for this error, and how to fix it?
2
Answers
You haven’t added
FirstPage
to theMaterialApp
. Add it as the argument to thehome
parameter.You should wrap your "FirstPage" widget inside the "home:" property in the material app. Its like you have defined your funtion that will print "hello world" but you are not invoking it in "void main()" the sample code for doing this is given below. Hope this helps.
Thank you.
MaterialApp( // ... home: const FirstPage(), //... )