Search code examples
androidflutterdart

Getting this Error: The method 'SignInPage' isn't defined for the class 'MyApp'


I am new to flutter I am trying to implement google auth and have another file named sign_in_page.dart. I am getting the below error when trying to add this in main.dart

Error: The method 'SignInPage' isn't defined for the class
'MyApp'.
 - 'MyApp' is from 'package:walkie/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method
named 'SignInPage'.
      home: SignInPage(),  // Use the imported SignInPage here

Here are my files:

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'sign_in_page.dart';  // Add this import

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: const FirebaseOptions();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Google Sign In',
      home: SignInPage(),  // Use the imported SignInPage here
    );
  }
}


import 'package:flutter/material.dart';

class SignInPage extends StatelessWidget {
  const SignInPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Sign In'),
      ),
      body: Center(
        child: Text('This is a placeholder for the Sign In page'),
      ),
    );
  }
}

If I direct build the signup in the main.dart itself then I don't get any error and works totally fine but if I create seperate file then get the error.


Solution

  • You need to import the file with correct path import package:walkie/sign_in_page.dart'; or you can use the relative path and the 3rd option is if you are using a VS code tool, then when you will click on the quick fix after removing the import 'sign_in_page.dart'; // Add this import then the ide also give you the option to add the correct path