Flutter 未处理的异常:getter';名称';被调用为空
Hooolaaa社区,我这里有一个非常有趣的问题,我真的不明白, 我定义了一个字符串变量(String uName=“”;),并通过以下方式将当前用户名传递给它(uName=userCurrentInfo.name;),然后在2个文本小部件中使用(Text(“${userCurrentInfo?.name}”,style:TextStyle(fontSize:16.0,fontfamine:“Brand Bold”), 问题是: 有时它不会显示用户名,直到我重新启动android studio并卸载我的应用程序并重新启动,然后它才开始工作。而另一些时候它就可以工作了。 我所尝试的: 我曾尝试直接从firebase的currentUser name调用,如下:(Text(${userCurrentInfo?.name})和这项工作,但我不想每次使用当前用户的名称时都重复这一点。 谢谢你的时间 以下是错误消息: 未处理的异常:NoSuchMethodError:对null调用了getter“name”。 收件人:空Flutter 未处理的异常:getter';名称';被调用为空,flutter,dart,firebase-realtime-database,firebase-authentication,Flutter,Dart,Firebase Realtime Database,Firebase Authentication,Hooolaaa社区,我这里有一个非常有趣的问题,我真的不明白, 我定义了一个字符串变量(String uName=“”;),并通过以下方式将当前用户名传递给它(uName=userCurrentInfo.name;),然后在2个文本小部件中使用(Text(“${userCurrentInfo?.name}”,style:TextStyle(fontSize:16.0,fontfamine:“Brand Bold”), 问题是: 有时它不会显示用户名,直到我重新启动android studio并
class MainScreen extends StatefulWidget
{
static const String idScreen = "mainScreen";
@override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> with TickerProviderStateMixin
{
String uName="";
void locatePosition() async
{
Position position = await Geolocator.getCurrentPosition(desiredAccuracy:
LocationAccuracy.high);
currentPosition = position;
LatLng latLatPosition = LatLng(position.latitude, position.longitude);
String address = await AssistantMethods.searchCoordinateAddress(position, context);
print("This is your Address :: " + address);
initGeoFireListner();
=====> uName = userCurrentInfo.name; // I have user name stored in my uName String Variable.
appFirebaseMonitoring();
AssistantMethods.retrieveHistoryInfo(context);
}
// I used it my Drawer in the build method
@override
Widget build(BuildContext context) {
createIconMarker();
return Scaffold(
key: scaffoldKey,
drawer: Container(
color: Colors.white,
width: 255.0,
child: Drawer(
child: ListView(
children: [
//Drawer Header
Container(
height: 165.0,
child: DrawerHeader(
decoration: BoxDecoration(color: Colors.white),
child: Row(
children: [
Image.asset("images/user_icon.png", height: 65.0, width: 65.0,),
SizedBox(width: 16.0,),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(uName, style: TextStyle(fontSize: 16.0, fontFamily: "Brand Bold"),),
SizedBox(height: 6.0,),
GestureDetector(
onTap: ()
{
Navigator.push(context, MaterialPageRoute(builder: (context)=> ProfileTabPage()));
},
child: Text("Visit Profile")
),
],
),
),
],
),
),
),
class主屏幕扩展StatefulWidget
{
静态常量字符串idScreen=“mainScreen”;
@凌驾
_MainScreenState createState()=>\u MainScreenState();
}
类_MainScreenState使用TickerProviderStateMixin扩展状态
{
字符串uName=“”;
void locatePosition()异步
{
位置位置=等待地理定位器。getCurrentPosition(期望精度:
定位精度高;
当前位置=位置;
LatLng latLatPosition=LatLng(位置.纬度,位置.经度);
字符串地址=Wait AssistantMethods.searchCoordinateAddress(位置、上下文);
打印(“这是您的地址::”+地址);
initGeoFireListner();
====>uName=userCurrentInfo.name;//我的uName字符串变量中存储了用户名。
appFirebaseMonitoring();
辅助方法。检索历史信息(上下文);
}
//我在构建方法中使用了它
@凌驾
小部件构建(构建上下文){
createIconMarker();
返回脚手架(
钥匙:脚手架钥匙,
抽屉:集装箱(
颜色:颜色,白色,
宽度:255.0,
孩子:抽屉(
子:ListView(
儿童:[
//抽屉头
容器(
身高:165.0,
儿童:抽屉阅读器(
装饰:盒子装饰(颜色:彩色。白色),
孩子:排(
儿童:[
资产(“images/user_icon.png”,高度:65.0,宽度:65.0,),
尺寸控制盒(宽度:16.0,),
扩大(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(uName,样式:TextStyle(fontSize:16.0,fontFamily:“品牌粗体”),
尺寸箱(高度:6.0,),
手势检测器(
onTap:()
{
push(context,materialpage(builder:(context)=>ProfileTabPage());
},
子项:文本(“访问配置文件”)
),
],
),
),
],
),
),
),
在flatter中,您需要手动更新小部件的状态。这意味着如果您有一个变量,如var name=“temp”
并且您构建了一个小部件,如Text(name)
它将显示temp
。然后,如果将name
变量更改为myName
,文本仍将显示temp
,这是因为需要用新值重新绘制要构建的布局
在StatefulWidget
的State
中,您有状态(变量),当它们改变时,您需要调用setState
setState
将使用新状态(变量)重新绘制布局:
void locatePosition()异步{
位置位置=等待地理定位器。getCurrentPosition(期望精度:
定位精度高;
当前位置=位置;
LatLng latLatPosition=LatLng(位置.纬度,位置.经度);
字符串地址=Wait AssistantMethods.searchCoordinateAddress(位置、上下文);
打印(“这是您的地址::”+地址);
initGeoFireListner();
setState((){//使用此更改重新生成此小部件
uName=userCurrentInfo.name;
});
appFirebaseMonitoring();
辅助方法。检索历史信息(上下文);
}
我也没有看到您在哪里调用locatePosition
调用它的最简单方法是在initState
内部\u MainScreenState
:
@覆盖
void initState(){
super.initState();
locatePosition();
}
如果
userCurrentInfo?.name
有效,那么您可以做两件事:
uName
成为全局getter:uName
为userCurrentInfo?.name
:字符串uName=”“;
@凌驾
void initState(){
super.initState();
uName=userCurrentInfo?.name??“”;
}
好的,我试过这样做:setState((){//使用以下更改重新生成此小部件uName=userCurrentInfo.name;});它不起作用,而且initState None work我的代码共享整个主屏幕代码,只是它的1300行代码缺少很多东西。比如,无论createIconMarker
做什么,你都不应该在build
内部调用它。userCurrentInfo
从哪里来?它是静态全局变量吗?如果在这种情况下,您可以使用顶级getter获取用户名和