skip to Main Content

I am not a professional in this i have just started my very first project to make an app.. i started with the most important and probably the most advanced thing about my app and that is the log-in function

i have set up a project in Firebase and did as i should but i still get this error.

I have a dog kennel and have interest in coding so i was trying to combine those two for a neat way to find my dogs competition results and information about them. I think i maybe i am way over my head in this but i have been in front of the computer in maybe 2 whole days trying to resolve this.

sorry for typos, i’m a "Hermit" living in the norwegian woods

ERROR: Sign In Error: TypeError: Instance of ‘FirebaseException’: type ‘FirebaseException’ is not a subtype of type ‘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

Started over a few times, asked chatgpt (with no luck), youtube, asked my wife (she is a backend programmer)

2

Answers


  1. I had same issue. In my case I put this snipped to main.dart and now working well.

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

    My main.dart file looks like this

    main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
    );
    //....  
    runApp(const MainApp());
    }
    
    Login or Signup to reply.
  2. You need to initialize Firebase before interacting with it. This error message means you called the Firebase.initializeapp method asynchronously or not at all.

    Examples:

    ❌Bad:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      runApp(const MyApp());
    }
    

    ❌Bad:

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

    ✅Good:

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

    Note: The first option could theoretically work if you waited long enough for it to initialize as well as could the second one if you initialized it later (in a loader widget for example) before using Firebase.

    Since you have the WidgetsFlutterBinding.ensureInitialized(); in the main method I assume you accidentally deleted the

    await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

    which was supposed to be on the next line.

    You will also have to import the firebase_options.dart generated by flutterfire – more info on that here: https://firebase.google.com/docs/flutter/setup

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