firebase / flutterfire

šŸ”„ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.71k stars 3.97k forks source link

[firebase_auth]: <windows firebase flutter failure> #13340

Closed NajdKarajeh closed 1 month ago

NajdKarajeh commented 1 month ago

Is there an existing issue for this?

Which plugins are affected?

Auth

Which platforms are affected?

Windows

Description

[ERROR:flutter/shell/common/shell.cc(1065)] The 'firebase_auth_plugin/auth-state/[DEFAULT]' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.

Reproducing the issue

the description i provided says that channel sent a message from native to flutter on an non-platform channel messages..i do handle this on my code correct but i still get this warrning

Firebase Core version

3.4.1

Flutter Version

3.24.0

Relevant Log Output

No response

Flutter dependencies

n flutter: sdk: flutter http: ^1.2.2 provider: ^6.0.0 image_picker: ^1.1.2 cloud_firestore: 5.4.1 firebase_analytics : 11.3.1 firebase_auth : 5.2.1 firebase_core : 3.4.1 intl : 0.19.0

shared_preferences : 2.3.2 connectivity_plus: 6.0.5 fluttertoast: 8.2.8 font_awesome_flutter: 10.7.0 google_sign_in: 6.2.1

SelaseKay commented 1 month ago

Kindly share a sample reproducible code

NajdKarajeh commented 1 month ago

firebase_auth_services.txt FormContainerWidget.txt LoginPage.txt main.txt SplashScreen.txt toast.txt

@SelaseKay

SelaseKay commented 1 month ago

Kindly share the code in the comment rather than sharing it as a file.

NajdKarajeh commented 1 month ago

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

import 'package:cashgo/toast.dart';

class FirebaseAuthService { FirebaseAuth _auth = FirebaseAuth.instance;

Future<User?> signUpWithEmailAndPassword(BuildContext context, String email, String password) async { try { UserCredential credential = await _auth.createUserWithEmailAndPassword(email: email, password: password);

  // Ensure UI updates happen on the main thread
  WidgetsBinding.instance.addPostFrameCallback((_) {
    showCustomSnackBar(context, 'User is successfully created.');
  });
  return credential.user;
} on FirebaseAuthException catch (e) {
  WidgetsBinding.instance.addPostFrameCallback((_) {
    if (e.code == 'email-already-in-use') {
      showCustomSnackBar(context, 'The email address is already in use.');
    } else {
      showCustomSnackBar(context, 'An error occurred: ${e.code}');
    }
  });
}
return null;

}

Future<User?> signInWithEmailAndPassword(BuildContext context, String email, String password) async { try { UserCredential credential = await _auth.signInWithEmailAndPassword(email: email, password: password);

  // Ensure UI updates happen on the main thread
  WidgetsBinding.instance.addPostFrameCallback((_) {
    showCustomSnackBar(context, 'User is successfully signed in.');
  });
  return credential.user;
} on FirebaseAuthException catch (e) {
  WidgetsBinding.instance.addPostFrameCallback((_) {
    if (e.code == 'user-not-found' || e.code == 'wrong-password') {
      showCustomSnackBar(context, 'Invalid email or password.');
    } else {
      showCustomSnackBar(context, 'An error occurred: ${e.code}');
    }
  });
}
return null;

} }


import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'firebase_options.dart'; import 'screens/supermarket_cashier_screen.dart'; import 'package:flutter/foundation.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:cashgo/Other/splashscreen.dart'; import 'package:cashgo/LoginPage/LoginPage.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; // Import Firestore

Future main() async { WidgetsFlutterBinding.ensureInitialized();

// Initialize Firebase and handle errors bool firebaseInitialized = false; String firebaseErrorMessage = '';

try { // Initialize Firebase using platform-specific options from firebase_options.dart await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, );

// Enable Firestore offline persistence
FirebaseFirestore.instance.settings = const Settings(
  persistenceEnabled: true,
);

// Listen to Firebase Auth changes and ensure it runs on the UI thread
FirebaseAuth.instance.idTokenChanges().listen((User? user) {
  WidgetsBinding.instance.addPostFrameCallback((_) {
    // Handle auth changes, ensure it's on the main thread
    if (user != null) {
      print("User is logged in: ${user.email}");
    } else {
      print("User is logged out.");
    }
  });
});

firebaseInitialized = true;

} catch (e) { firebaseErrorMessage = e.toString(); }

runApp(MyApp( firebaseInitialized: firebaseInitialized, firebaseErrorMessage: firebaseErrorMessage, )); }

class MyApp extends StatelessWidget { final bool firebaseInitialized; final String firebaseErrorMessage;

const MyApp({ super.key, required this.firebaseInitialized, required this.firebaseErrorMessage, });

@override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Supermarket Cashier System', routes: { '/': (context) => SplashScreen( child: _buildInitialScreen(), // Build the initial screen based on user auth ), '/login': (context) => LoginPage(), '/cashier': (context) => SupermarketCashierScreen( firebaseInitialized: firebaseInitialized, firebaseErrorMessage: firebaseErrorMessage, ), }, ); }

// This function decides what the initial screen is based on authentication state Widget _buildInitialScreen() { if (!firebaseInitialized) { // If Firebase is not initialized, show a loading spinner return const Center( child: CircularProgressIndicator(), ); }

final user = FirebaseAuth.instance.currentUser;

if (user != null) {
  // If the user is already logged in, navigate to the cashier screen
  return SupermarketCashierScreen(
    firebaseInitialized: firebaseInitialized,
    firebaseErrorMessage: firebaseErrorMessage,
  );
} else {
  // If the user is not logged in, navigate to the login page
  return LoginPage();
}

} }


import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:cashgo/Other/FormContainerWidget.dart'; import 'package:cashgo/toast.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'package:cashgo/firebase_auth/firebase_auth_services.dart';

class LoginPage extends StatefulWidget { const LoginPage({super.key});

@override State createState() => _LoginPageState(); }

class _LoginPageState extends State { bool _isSigning = false; final FirebaseAuthService _auth = FirebaseAuthService(); final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; TextEditingController _emailController = TextEditingController(); TextEditingController _passwordController = TextEditingController();

@override void dispose() { _emailController.dispose(); _passwordController.dispose(); super.dispose(); }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( automaticallyImplyLeading: false, title: const Text(""), ), body: Center( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 15), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( "Login", style: TextStyle(fontSize: 27, fontWeight: FontWeight.bold), ), const SizedBox(height: 30), FormContainerWidget( controller: _emailController, hintText: "Email", isPasswordField: false, ), const SizedBox(height: 10), FormContainerWidget( controller: _passwordController, hintText: "Password", isPasswordField: true, ), const SizedBox(height: 30), GestureDetector( onTap: () { _signIn(); }, child: Container( width: double.infinity, height: 45, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(10), ), child: Center( child: _isSigning ? const CircularProgressIndicator( color: Colors.white, ) : const Text( "Login", style: TextStyle( color: Colors.white, fontWeight: FontWeight.bold, ), ), ), ), ), const SizedBox(height: 10), GestureDetector( child: Container( child: Center( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: const [ ], ), ), ), ), const SizedBox(height: 20),

        ],
      ),
    ),
  ),
);

}

void _signIn() async { setState(() { _isSigning = true; });

String email = _emailController.text;
String password = _passwordController.text;

User? user = await _auth.signInWithEmailAndPassword(context, email, password);

setState(() {
  _isSigning = false;
});

if (user != null) {
  showCustomSnackBar(context, "User successfully signed in!");
  Navigator.pushReplacementNamed(context, '/cashier'); // Navigate to the cashier screen
} else {
  showCustomSnackBar(context, "An error occurred during login");
}

}

}


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

void showCustomSnackBar(BuildContext context, String message) { final snackBar = SnackBar( content: Text(message), duration: Duration(seconds: 2), // The duration for how long the message will stay backgroundColor: Colors.blue, behavior: SnackBarBehavior.floating, // Makes it float over the content );

ScaffoldMessenger.of(context).showSnackBar(snackBar); }

@SelaseKay

SelaseKay commented 1 month ago

This could be an OS issue rather than FF plugin issue. Kindly try running the firebase_auth example on windows and provide feedback.

NajdKarajeh commented 1 month ago

@SelaseKay all of my system is working perfectly i just want to know what's wrong with this warning note that's it and my firebase is working 100% with my flutter app

testing for now on windows I'm plaining to do the app to work for android/iOS and windows.

SelaseKay commented 1 month ago

I understand. Could you please try running the firebase_auth example app to see if the same warning message appears on your end?

NajdKarajeh commented 1 month ago

aah i see that will prove it okey,,,where i find this example app?

SelaseKay commented 1 month ago

https://github.com/firebase/flutterfire/tree/main/packages/firebase_auth/firebase_auth/example

google-oss-bot commented 1 month ago

Hey @NajdKarajeh. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 1 month ago

Since there haven't been any recent updates here, I am going to close this issue.

@NajdKarajeh if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.