即使应用程序';s所有入口点都已初始化其firebase_核心
正如标题所示,我以前遇到并解决了这个问题,直到我在我的应用程序中实现了一个新功能,我才再遇到这个问题。我不明白是什么原因导致抛出异常,欢迎提供任何评论/答案 New function=如果数据库没有数据,则显示一个alertdialog供用户输入全名,以便我知道哪个登录属于谁,并将名称传递到数据库中,以便他们可以直接登录,应用程序将从数据库中读取。(使用SharedReferences和Firebase存储用户数据) 下面是我的代码: 主飞镖即使应用程序';s所有入口点都已初始化其firebase_核心,firebase,flutter,exception,Firebase,Flutter,Exception,正如标题所示,我以前遇到并解决了这个问题,直到我在我的应用程序中实现了一个新功能,我才再遇到这个问题。我不明白是什么原因导致抛出异常,欢迎提供任何评论/答案 New function=如果数据库没有数据,则显示一个alertdialog供用户输入全名,以便我知道哪个登录属于谁,并将名称传递到数据库中,以便他们可以直接登录,应用程序将从数据库中读取。(使用SharedReferences和Firebase存储用户数据) 下面是我的代码: 主飞镖 Future<void> main()
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var storeddisplayName = prefs.getString('displayName');
var storedgooglename = prefs.getString('googlename');
var storedemail = prefs.getString('email');
var storedimage = prefs.getString('imageUrlGoogle');
var storedid = prefs.getString('id');
print(
'name : $storeddisplayName,google name : $storedgooglename ,email : $storedemail,image : $storedimage,id : $storedid');
runApp(MaterialApp(
home: storedid == null ? App() : InitializedFireMyApp(),
debugShowCheckedModeBanner: false));
}
class App extends StatefulWidget {
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
// Set default `_initialized` and `_error` state to false
bool _initialized = false;
bool _error = false;
// Define an async function to initialize FlutterFire
void initializeFlutterFire() async {
try {
// Wait for Firebase to initialize and set `_initialized` state to true
await Firebase.initializeApp();
setState(() {
_initialized = true;
});
} catch (e) {
// Set `_error` state to true if Firebase initialization fails
setState(() {
_error = true;
});
}
}
@override
void initState() {
initializeFlutterFire();
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(home: LoginPage(), debugShowCheckedModeBanner: false);
}
}
class InitializedFireMyApp extends StatefulWidget {
@override
_InitializedFireMyAppState createState() => _InitializedFireMyAppState();
}
class _InitializedFireMyAppState extends State<InitializedFireMyApp> {
// Set default `_initialized` and `_error` state to false
bool _initialized = false;
bool _error = false;
// Define an async function to initialize FlutterFire
void initializeFlutterFire() async {
try {
// Wait for Firebase to initialize and set `_initialized` state to true
await Firebase.initializeApp();
setState(() {
_initialized = true;
});
} catch (e) {
// Set `_error` state to true if Firebase initialization fails
setState(() {
_error = true;
});
}
}
@override
void initState() {
initializeFlutterFire();
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(home: MyApp(), debugShowCheckedModeBanner: false);
}
}
很抱歉,我好像在这里扔了很多东西,但我真的不知道是什么导致了这个问题
如果您需要更多信息,请告诉我
class SignInButton extends StatefulWidget {
@override
_SignInButtonState createState() => _SignInButtonState();
}
class _SignInButtonState extends State<SignInButton> {
@override
Widget build(BuildContext context) {
return OutlineButton(
highlightedBorderColor: Colors.white,
splashColor: Colors.grey,
onPressed: () async {
signInWithGoogle().then((_) async {
SharedPreferences prefs2 = await SharedPreferences.getInstance();
prefs2.setString('googlename', name);
prefs2.setString('email', email);
prefs2.setString('imageUrlGoogle', imageUrlGoogle);
prefs2.setString('id', id);
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return MyApp();
},
),
);
});
},
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
highlightElevation: 0,
borderSide: BorderSide(color: Colors.grey),
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 7, 0, 7),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 0),
child: Center(
child: Image(
image: AssetImage('images/google_logo.png'),
height: 40,
),
),
)
],
),
),
);
}
}
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.black,
body: Container(
color: Colors.grey[850],
child: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('images/logo.png'),
Text('CYC Conference App',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontFamily: 'Spartan',
fontWeight: FontWeight.bold,
fontSize: 30)),
SizedBox(height: 50),
Text('Sign in with Google',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white54,
fontFamily: 'Spartan',
fontWeight: FontWeight.bold,
fontSize: 20)),
SizedBox(
height: 10,
),
SignInButton(),
],
),
),
),
));
}
}
@override //MyApp()'s initState
void initState() {
// TODO: implement initState
super.initState();
testConnectionLogin(context);
}
testConnectionLogin(context) async {
try {
final result = await InternetAddress.lookup('example.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
SharedPreferences prefs = await SharedPreferences.getInstance();
print('IT IS CONNECTED BOYS');
setDisplayName(BuildContext context) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final GlobalKey<FormState> _nameFormKey = GlobalKey<FormState>();
showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
title: Text('Full name :'),
content: Form(
key: _nameFormKey,
child: TextFormField(
controller: displayNameController,
validator: (value) {
if (value.isEmpty) {
// name is empty, don't validate
return 'Please set your full name';
} else if (value.split(' ').length < 2) {
// name contains only 1 word, don't validate
return 'Only 1 word, please set your full name';
} else if (value.length < 5) {
// name is too short, don't validate
return 'Name too short, please set your full name';
}
// validate
return null;
},
),
),
actions: <Widget>[
new FlatButton(
child: Text('CONFIRM'),
onPressed: () {
if (_nameFormKey.currentState.validate()) {
prefs.setString(
'displayName', displayNameController.text);
Navigator.of(context).pop();
displayNameController.clear();
print('validated');
submitUserDisplayName();
}
},
)
],
));
});
}
print('HERE');
String _gName = prefs.getString('googlename').toString();
String _dName = prefs.getString('displayName').toString();
String _docTitle = _gName + ' - '+_dName;
await dbref
.collection("userData")
.doc(_docTitle)
.get()
.then((data) async {
if (!((prefs.getString('googlename') == data.get('GoogleName')) &&
prefs.getString('displayName') == data.get('displayName'))) {
setDisplayName(context);
}
});
}
} on SocketException catch (_) {
Scaffold.of(context).showSnackBar(snackBar3);
print('Sign in not complete, no internet Connection');
}
}
void submitUserDisplayName() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await dbref
.collection('userData')
.doc(prefs.getString('googlename') +
' - ' +
prefs.getString('displayName'))
.set({
'GoogleName': prefs.getString('displayName'),
'DisplayName': prefs.getString('displayName'),
'UID': prefs.getString('id'),
'imagelink': prefs.getString('imageUrlGoogle'),
});
}
E/flutter (19121): #38 Element.updateChild
package:flutter/…/widgets/framework.dart:3266
E/flutter (19121): #39 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4589
E/flutter (19121): #40 Element.rebuild
package:flutter/…/widgets/framework.dart:4280
E/flutter (19121): #41 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4543
E/flutter (19121): #42 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4538
E/flutter (19121): #43 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3508
E/flutter (19121): #44 Element.updateChild
package:flutter/…/widgets/framework.dart:3266
E/flutter (19121): #45 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4589
E/flutter (19121): #46 StatefulElement.performRebuild (pac