出现此错误:未为类“MyApp”定义方法“SignInPage”

问题描述 投票:0回答:1

我是颤振新手 我正在尝试实现 google auth 并有另一个名为sign_in_page.dart 的文件。当我尝试将其添加到 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

这是我的文件:

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(apiKey: "AIzaSyA5DMMyWsuAPhKR72euVZxj7cyG_yI0DME", appId: "1:765106867233:android:4983af9b13846a4894e760", messagingSenderId: "765106867233", projectId: "walkgame-23313")
  );
  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'),
      ),
    );
  }
}

如果我直接在 main.dart 本身中构建注册,那么我不会收到任何错误并且工作完全正常,但如果我创建单独的文件则会收到错误。

android flutter dart
1个回答
0
投票

import 'sign_in_page.dart';
更改为
import package:walkie/sign_in_page.dart';
(如果sign_in_page.dart与MyApp.dart位于同一目录中;否则设置正确的完整路径)

无论如何,我建议您使用 VScode 或其他正确配置的 IDE,因为它将提供轻松避免这些错误的工具。

© www.soinside.com 2019 - 2024. All rights reserved.