gpt4 book ai didi

firebase - Flutter FutureBuilder会引发错误,但在仿真中它会按预期工作

转载 作者:行者123 更新时间:2023-12-03 04:21:22 26 4
gpt4 key购买 nike

我是Flutter的初学者,目前在Flutter中正确实现FutureBuilder时遇到问题。
我正在尝试建立一个用户页面,我的用户信息存储在Firebase中,每次访问该用户页面时,它都会检索当前用户数据并在页面上显示数据。这是我为实现编写的代码:

class UserPage extends StatefulWidget{
@override
UserPageState createState() => UserPageState();
}

class UserPageState extends State<UserPage>{
String userName;
String userEmail;
String collegeName;

Future _infoInit() async {
userName = await HelperFunctions.getUserNamePreference();
userEmail = await HelperFunctions.getUserEmailPreference();
collegeName = await HelperFunctions.getUserCollegePreference();
}

Widget userScaffold(BuildContext context, AsyncSnapshot snapshot){
return Scaffold(
appBar: AppBar(
elevation: 0,
title: Text(
userName
),
backgroundColor: Colors.lightBlue,
),
body:Center(
child: Text("This is User Page")
)
);
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _infoInit(),
builder: (context,AsyncSnapshot snapshot) => userScaffold(context, snapshot)
);
}
}


目前,我编写的唯一部分是在应用程序栏上显示当前登录的用户,并且当我运行代码时,它似乎已成功运行。但是,当我查看android studio控制台时,我可以看到它实际上正面临着错误,我认为这与在FutureBuilder小部件中执行的异步功能有关。

enter image description here

Error Message:
Performing hot reload...
Syncing files to device iPhone 11...
Reloaded 7 of 650 libraries in 397ms.

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building FutureBuilder<dynamic>(dirty, state: _FutureBuilderState<dynamic>#772f1):
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 298 pos 10: 'data != null'

The relevant error-causing widget was:
FutureBuilder<dynamic> file:///Users/nossu3751/Downloads/flutter_project/moim_app/lib/user/profile.dart:39:12
When the exception was thrown, this was the stack:
#2 new Text (package:flutter/src/widgets/text.dart:298:10)
#3 UserPageState.userScaffold (package:moimapp/user/profile.dart:26:18)
#4 UserPageState.build.<anonymous closure> (package:moimapp/user/profile.dart:41:54)
#5 _FutureBuilderState.build (package:flutter/src/widgets/async.dart:732:55)
#6 StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)

更确切地说,它表示我要在FutureBuilder中使用的userName为null,即使我相信我已经通过运行_infoInit()方法分配了该值,并且实际上确实在模拟器上正确显示了该值。

如果有人可以让我知道我在这里做错了什么,以及我可以做些什么来阻止此消息再次出现,我将不胜感激。提前非常感谢您!

最佳答案

您需要在构建器中使用ConnectionState。查看以下代码模板:(当前,您的构建器返回userScaffold,而无需等待将来完成)

return FutureBuilder(
future: yourFuture(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// future complete
// if error or data is false return error widget
if (snapshot.hasError) {
return _buildErrorWidget('SOMETHING WENT WRONG, TAP TO RELOAD');
}

// return data widget
return _buildDataWidget();

// return loading widget while connection state is active
} else
return _buildLoadingWidget();
},
);

关于firebase - Flutter FutureBuilder会引发错误,但在仿真中它会按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62444037/

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