gpt4 book ai didi

flutter - ServicesBinding.defaultBinaryMessenger 在绑定(bind)初始化之前被访问。 E/flutter ​​ (21284) :

转载 作者:行者123 更新时间:2023-12-02 03:04:10 25 4
gpt4 key购买 nike

如何解决这个问题?我需要检查用户是否登录

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized. E/flutter (21284): If you're running an application and need to access the binary messenger before runApp() has been called (for example, during plugin initialization), then you need to explicitly call the WidgetsFlutterBinding.ensureInitialized() first. E/flutter (21284): If you're running a test, you can call the TestWidgetsFlutterBinding.ensureInitialized() as the first line in your test's main() method to initialize the binding. E/flutter (21284): #0 defaultBinaryMessenger. (package:flutter/src/services/binary_messenger.dart:76:7) E/flutter (21284): #1 defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4) E/flutter (21284): #2 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62) E/flutter (21284): #3 MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:368:5) E/flutter (21284): #4 new FirebaseAuth._ (package:firebase_auth/src/firebase_auth.dart:15:13) E/flutter (21284): #5 FirebaseAuth.instance (package:firebase_auth/src/firebase_auth.dart:25:53) E/flutter (21284): #6 FirebaseAuth.instance (package:firebase_auth/src/firebase_auth.dart:25:29) E/flutter (21284): #7 main (package:alqaiser/main.dart:44:36) E/flutter (21284): #8 _runMainZoned.. (dart:ui/hooks.dart:239:25) E/flutter (21284): #9 _rootRun (dart:async/zone.dart:1126:13) E/flutter (21284): #10
_CustomZone.run (dart:async/zone.dart:1023:19) E/flutter (21284): #11 _runZoned (dart:async/zone.dart:1518:10) E/flutter (21284): #12 runZoned (dart:async/zone.dart:1502:12) E/flutter (21284): #13
_runMainZoned. (dart:ui/hooks.dart:231:5) E/flutter (21284): #14 _startIsolate. (dart:isolate-patch/isolate_patch.dart:307:19) E/flutter (21284): #15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12) E/flutter (21284):

This is my man function

void main() async {
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(e.toString() + ";"));
});
}

最佳答案

您可以将 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),
),
);
}
}

关于flutter - ServicesBinding.defaultBinaryMessenger 在绑定(bind)初始化之前被访问。 E/flutter ​​ (21284) :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59402626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com