未处理的异常:在 null 时调用了 getter 'name'



Hooolaaa Community, 在这里我有一个非常有趣的问题,我不太明白, 我定义了一个字符串变量(字符串uName = ";),我通过执行(uName = userCurrentInfo.name;)将我当前的用户名传递给它,之后我在2个文本小部件(Text('${userCurrentInfo?.name}', style: TextStyle(fontSize: 16.0, fontFamily: "Brand Bold"),), 问题 : 它有时不显示用户名,直到我重新启动我的 android 工作室并卸载我的应用程序并进行全新重启,然后它才开始工作。而其他时候它只是工作。 我尝试过的: 我尝试直接从 firebase 的当前用户名调用,如下所示:( Text('${userCurrentInfo?.name}',) 和这项工作,但我不想每次我想使用当前用户的名称时都重复这一点。 感谢您抽出宝贵时间

以下是错误消息: 未处理的异常:NoSuchMethod错误:在 null 上调用了获取者"name"。 接收器:空

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")
),
],
),
),
],
),
),
),

在 Flutter 中,您需要手动更新小部件的状态。 这意味着,如果你有一个像var name = "temp"这样的变量,并且你构建了一个像Text(name)这样的小部件,它将显示temp。然后,如果将name变量更改为myName,则文本仍将显示temp,这是因为您需要重新绘制布局以使用新值构建。

StatefulWidgetState,你有状态(变量),当它们发生变化时,你需要调用setStatesetState将使用新状态(变量)重绘布局:

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();
setState((){ // rebuild this widget with this changes
uName =  userCurrentInfo.name;
});
appFirebaseMonitoring();
AssistantMethods.retrieveHistoryInfo(context);
}

我也没看到你叫locatePosition哪里?

最简单的调用方法是在_MainScreenState内部initState

@override
void initState() {
super.initState();
locatePosition();
}

如果userCurrentInfo?.name有效,那么您可以做两件事:

  1. uName成为全球获取者:
import '...';
String get uName => userCurrentInfo?.name ?? '';
class ... {
...
}
  1. uName初始化为userCurrentInfo?.name
String uName = "";
@override
void initState() {
super.initState();
uName =  userCurrentInfo?.name ?? '';
}

最新更新