skip to Main Content

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


  1. You haven’t added FirstPage to the MaterialApp. Add it as the argument to the home parameter.

    MaterialApp(
      // ...
      home: const FirstPage(),
    )
    
    Login or Signup to reply.
  2. 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(), //... )

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search