遇到登录错误问题:TypeError:“FirebaseException”实例

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

我不是这方面的专业人士,我刚刚开始我的第一个项目来制作应用程序..我从我的应用程序中最重要且可能是最先进的事情开始,那就是登录功能

我已经在 Firebase 中设置了一个项目,并按照我应该做的那样做了,但我仍然收到此错误。

我有一个狗舍,并且对编码感兴趣,所以我试图将这两者结合起来,以一种巧妙的方式来查找我的狗的比赛结果和有关它们的信息。我想我可能在这方面超出了我的想象,但我可能在电脑前花了整整两天的时间试图解决这个问题。

抱歉拼写错误,我是住在挪威森林里的“隐士”

错误:登录错误:TypeError:“FirebaseException”实例:“FirebaseException”类型不是“JavaScriptObject”类型的子类型

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dog Results App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SignInScreen(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Sign In'),
      ),
      body: SignInForm(),
    );
  }
}

class SignInForm extends StatefulWidget {
  const SignInForm({Key? key}) : super(key: key);

  @override
  _SignInFormState createState() => _SignInFormState();
}

class _SignInFormState extends State<SignInForm> {
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          TextField(
            controller: _emailController,
            decoration: const InputDecoration(labelText: 'Email'),
          ),
          TextField(
            controller: _passwordController,
            decoration: const InputDecoration(labelText: 'Password'),
            obscureText: true,
          ),
          ElevatedButton(
            onPressed: () {
              _signInWithEmailAndPassword(
                _emailController.text,
                _passwordController.text,
              );
            },
            child: const Text('Sign In'),
          ),
          TextButton(
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => const SignUpScreen()),
              );
            },
            child: const Text('Create Account'),
          ),
        ],
      ),
    );
  }

  Future<void> _signInWithEmailAndPassword(String email, String password) async {
    try {
      await FirebaseAuth.instance.signInWithEmailAndPassword(
        email: email,
        password: password,
      );
      Navigator.pushReplacement(
        context,
        MaterialPageRoute(builder: (context) => const HomeScreen()),
      );
    } catch (e) {
      debugPrint('Sign In Error: $e');
    }
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Sign Up'),
      ),
      body: const SignUpForm(),
    );
  }
}

class SignUpForm extends StatefulWidget {
  const SignUpForm({Key? key}) : super(key: key);

  @override
  _SignUpFormState createState() => _SignUpFormState();
}

class _SignUpFormState extends State<SignUpForm> {
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          TextField(
            controller: _emailController,
            decoration: const InputDecoration(labelText: 'Email'),
          ),
          TextField(
            controller: _passwordController,
            decoration: const InputDecoration(labelText: 'Password'),
            obscureText: true,
          ),
          ElevatedButton(
            onPressed: () {
              _createUserWithEmailAndPassword(
                _emailController.text,
                _passwordController.text,
              );
            },
            child: const Text('Sign Up'),
          ),
        ],
      ),
    );
  }

  Future<void> _createUserWithEmailAndPassword(String email, String password) async {
    try {
      await FirebaseAuth.instance.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
      Navigator.pop(context);
    } catch (e) {
      debugPrint('Sign Up Error: $e');
    }
  }
}

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

  @override
  Widget build(BuildContext context) {
    final user = FirebaseAuth.instance.currentUser;
    return Scaffold(
      appBar: AppBar(
        title: const Text('Welcome'),
      ),
      body: Center(
        child: Text('Hello, ${user!.email ?? "User"}'), // Ensure user is not null before accessing email
      ),
    );
  }
}

name: dog_results_app
description: "A new Flutter project."

environment:
  sdk: '>=2.12.0 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  firebase_core: 2.25.4
  firebase_auth: ^4.4.0
  cloud_firestore: 4.15.5
  logger: ^2.0.2+1  # Add the logger package

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^3.0.1  # Add flutter_lints package

flutter:
  uses-material-design: true

重新开始几次,问chatgpt(没有运气),youtube,问我妻子(她是后端程序员)

flutter firebase dart firebase-authentication
1个回答
0
投票

我也有同样的问题。就我而言,我将其剪裁到 main.dart 中,现在运行良好。

WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);

我的 main.dart 文件看起来像这样

main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
//....  
runApp(const MainApp());
}
© www.soinside.com 2019 - 2024. All rights reserved.