我正在构建一个Flutter应用程序,我已经集成了Firebase,但当我点击一个按钮注册,登录或注销时,我一直得到这个错误。我看到其他人也问过同样的问题,但似乎没有一个对我有用。我正在使用Flutter和Android Studio。我该如何解决这个问题?
这是我的代码的摘录
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red,
body: Center(
child: Container(
child: RaisedButton(
onPressed: () {
FirebaseAuth.instance.signOut().then((value) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
LoginScreen()));
});
},
child: Text("Logout"),
)
)
)
);
}
}
下面是抛出的异常
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
The following FirebaseException was thrown while handling a gesture:
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
When the exception was thrown, this was the stack:
#0 MethodChannelFirebase.app (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:118:5)
#1 Firebase.app (package:firebase_core/src/firebase.dart:52:41)
#2 FirebaseAuth.instance (package:firebase_auth/src/firebase_auth.dart:37:47)
#3 _HomeScreenState.build.<anonymous closure> (package:cosytok/screens/home.dart:20:28)
#4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
#5 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
#6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
#7 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
#8 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
#9 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:219:7)
#10 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:477:9)
#11 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:78:12)
#12 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:124:9)
#13 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
#14 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:122:18)
#15 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:108:7)
#16 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:220:19)
#17 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
#18 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
#19 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
#20 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
#24 _invoke1 (dart:ui/hooks.dart:267:10)
#25 _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5)
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer:
TapGestureRecognizer#f0104
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by gesture ═══════════════════════════════════════════════════════════════
The following FirebaseException was thrown while handling a gesture:
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
我需要用FlutterFire cli重新配置Firebase应用程序。
https://firebase.flutter.dev/docs/cli
通过NPM Install -g firebase-tools安装firebase-tool
安装flutterfire cli: dart pub global activate flutterfire_cli
运行此命令在项目中配置firebase: flutterfire configure
按上述链接进行初始化,如下所示:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
当它等待调用时,应用程序将显示启动屏幕,Firebase可能无法初始化,因此我接下来用另一个FutureBuilder作为主屏幕,以确保在使用FirebaseAuth之前,Firebase应用程序已初始化。
home: FutureBuilder(
future: Firebase.initializeApp(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text(snapshot.error.toString(),
style: TextStyle(fontSize: 10));
}
if (snapshot.connectionState == ConnectionState.done) {
return StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
return snapshot.hasData ? Home() : AuthPage();
},
);
} else {
return Text('');
}
})
颤动网与Firebase
如果您正在使用Flutter Web和Firebase,请确保在您的。/ Web /index.html的正文标签中安装了sdk
<script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-analytics.js"></script>
此外,确保使用index.html中的配置参数调用firebase.initializeApp(…)。
<script>
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
var firebaseConfig = {
apiKey: "AIz...",
authDomain: "...",
databaseURL: "https://<project-name>.firebaseio.com",
projectId: "...",
storageBucket: "<project-name>.appspot.com",
messagingSenderId: "...",
appId: "...",
measurementId: "..."
};
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
最后,按照Peter Haddad的回答配置firebase:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
我需要用FlutterFire cli重新配置Firebase应用程序。
https://firebase.flutter.dev/docs/cli
通过NPM Install -g firebase-tools安装firebase-tool
安装flutterfire cli: dart pub global activate flutterfire_cli
运行此命令在项目中配置firebase: flutterfire configure
按上述链接进行初始化,如下所示:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
当它等待调用时,应用程序将显示启动屏幕,Firebase可能无法初始化,因此我接下来用另一个FutureBuilder作为主屏幕,以确保在使用FirebaseAuth之前,Firebase应用程序已初始化。
home: FutureBuilder(
future: Firebase.initializeApp(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text(snapshot.error.toString(),
style: TextStyle(fontSize: 10));
}
if (snapshot.connectionState == ConnectionState.done) {
return StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
return snapshot.hasData ? Home() : AuthPage();
},
);
} else {
return Text('');
}
})
在大多数情况下,此问题是在将项目从旧的Flutter版本升级到零安全后观察到的。应该检查以下内容:
确保在pubspecc .yaml中有firebase-core
导入firebase核心:
import 'package:firebase_core/firebase_core.dart';
在任何调用之前Init Firebase -最好在main内部。Dart,因此它在任何其他实现之前执行:
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
来自官方文件。
只使用飞镖初始化(推荐)
在所有平台上使用CLI初始化Firebase。
不需要添加任何本地代码。
无需下载任何谷歌服务文件。
Install FlutterFire CLI
dart pub global activate flutterfire_cli
(Optional) Add the following line to your ~/.zshrc file and save it
export PATH="$PATH":"$HOME/.pub-cache/bin"
Run the configure:
flutterfire configure
Press enter to configure all the platforms
You should now have a lib/firebase_options.dart file. Import this file in your main.dart and use Firebase.initializeApp.
import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// This is the last thing you need to add.
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(...);
}
迁移到仅dart初始化:
如果您已经有一个现有的应用程序,您可以通过以下方式迁移它。
Android:
删除谷歌服务。Json文件从<flutter-project>/android/app:
删除<flutter-project>/android/build中的以下行。gradle文件
删除<flutter-project>/android/app/build中的以下行。gradle文件
iOS
打开< flutter-project > / ios /跑步者。在Xcode中删除GoogleService-Info文件。来自Runner的plist文件。
macOS
打开< flutter-project > / macos /跑步者。在Xcode中删除GoogleService-Info文件。来自Runner的plist文件。
更多信息可以在这里找到