Flutter 在初始化绑定之前已访问ServicesBinding.defaultBinaryMessenger。E/颤振(21284):
如何解决这个问题?我需要检查用户是否已登录 [错误:flatter/lib/ui/ui\u dart\u state.cc(157)]未处理的异常: 在绑定之前已访问ServicesBinding.defaultBinaryMessenger 已初始化。E/flatter(21284):如果您正在运行应用程序 并且需要在安装Flutter 在初始化绑定之前已访问ServicesBinding.defaultBinaryMessenger。E/颤振(21284):,flutter,Flutter,如何解决这个问题?我需要检查用户是否已登录 [错误:flatter/lib/ui/ui\u dart\u state.cc(157)]未处理的异常: 在绑定之前已访问ServicesBinding.defaultBinaryMessenger 已初始化。E/flatter(21284):如果您正在运行应用程序 并且需要在安装runApp()之前访问二进制messenger 调用(例如,在插件初始化期间),然后需要 首先显式调用WidgetsFlutterBinding.ensureInitial
runApp()
之前访问二进制messenger
调用(例如,在插件初始化期间),然后需要
首先显式调用WidgetsFlutterBinding.ensureInitialized()
。
E/flatter(21284):如果正在运行测试,可以调用
TestWidgetsFlutterBinding.ensureInitialized()
作为
测试的main()
方法初始化绑定。E/颤振
(21284):#0 defaultBinaryMessenger。
(软件包:颤振/src/services/binary_messenger.dart:76:7)E/颤振
(21284):#1个defaultBinaryMessenger
(软件包:颤振/src/services/binary_messenger.dart:89:4)E/颤振
(21284):#2 MethodChannel.binaryMessenger
(包装:颤振/src/服务/平台通道。dart:140:62)E/颤振
(21284):#3 MethodChannel.setMethodCallHandler
(包装:颤振/src/服务/平台通道。dart:368:5)E/颤振
(21284):#4个新FirebaseAuth_
(包装:firebase_auth/src/firebase_auth.dart:15:13)E/颤振
(21284):#5 FirebaseAuth.instance
(包装:firebase_auth/src/firebase_auth.dart:25:53)E/颤振
(21284):#6 FirebaseAuth.instance
(包装:firebase_auth/src/firebase_auth.dart:25:29)E/颤振
(21284):#7主(包装:alqaiser/main.dart:44:36)E/颤振
(21284):#8#。。
(飞镖:ui/钩子。飞镖:239:25)E/颤振(21284):#9#
(省道:异步/区域。省道:1126:13)E/颤振(21284):#10_CustomZone.run(dart:async/zone.dart:1023:19)E/flatter(21284):#11#runzone(dart:async/zone.dart:1518:10)E/flatter(21284):#12 runzone(dart:async/zone.dart:1502:12)E/flatter(21284):#13
_Runmain分区。(飞镖:ui/钩子。飞镖:231:5)E/颤振(21284):#14"星形。 (省道:隔离补片/隔离补片。省道:307:19)E/颤振(21284):#15 _RawReceivePortImpl._handleMessage(dart:isolate patch/isolate_patch.dart:174:12)E/颤振(21284):
这是我的男人功能
您可以放置
WidgetsFlutterBinding.ensureInitialized()代码>在第一行
代码片段
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseAuth auth = FirebaseAuth.instance;
auth.currentUser().then((u) {
if (u == null?true:u.phoneNumber==null?true:false) {
runApp(MaterialApp(
home: PhoneAuth(),
));
} else {
runApp(MyApp());
}
}).catchError((e) {
print(e);
runApp(MyAppT2());
});
}
完整测试代码
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseAuth auth = FirebaseAuth.instance;
auth.currentUser().then((u) {
if (u == null?true:u.phoneNumber==null?true:false) {
runApp(MaterialApp(
home: PhoneAuth(),
));
} else {
runApp(MyApp());
}
}).catchError((e) {
print(e);
runApp(MyAppT2());
});
}
class PhoneAuth extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container();
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyAppT2 extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“包:firebase_auth/firebase_auth.dart”;
void main()异步{
WidgetsFlutterBinding.ensureInitialized();
FirebaseAuth auth=FirebaseAuth.instance;
auth.currentUser()。然后((u){
如果(u==null?真:u.phoneNumber==null?真:假){
runApp(材料应用程序)(
主页:PhoneAuth(),
));
}否则{
runApp(MyApp());
}
}).catchError((e){
印刷品(e);
runApp(MyAppT2());
});
}
类PhoneAuth扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回容器();
}
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyAppT2扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),
正文(
“$”计数器“,
样式:Theme.of(context).textTheme.display1,
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
}
您可以将WidgetsFlutterBinding.ensureInitialized()代码>在第一行
代码片段
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseAuth auth = FirebaseAuth.instance;
auth.currentUser().then((u) {
if (u == null?true:u.phoneNumber==null?true:false) {
runApp(MaterialApp(
home: PhoneAuth(),
));
} else {
runApp(MyApp());
}
}).catchError((e) {
print(e);
runApp(MyAppT2());
});
}
完整测试代码
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseAuth auth = FirebaseAuth.instance;
auth.currentUser().then((u) {
if (u == null?true:u.phoneNumber==null?true:false) {
runApp(MaterialApp(
home: PhoneAuth(),
));
} else {
runApp(MyApp());
}
}).catchError((e) {
print(e);
runApp(MyAppT2());
});
}
class PhoneAuth extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container();
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyAppT2 extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“包:firebase_auth/firebase_auth.dart”;
void main()异步{
WidgetsFlutterBinding.ensureInitialized();
FirebaseAuth auth=FirebaseAuth.instance;
auth.currentUser()。然后((u){
如果(u==null?真:u.phoneNumber==null?真:假){
runApp(材料应用程序)(
主页:PhoneAuth(),
));
}否则{
runApp(MyApp());
}
}).catchError((e){
印刷品(e);
runApp(MyAppT2());
});
}
类PhoneAuth扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回容器();
}
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyAppT2扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(