flutter - Flutter Stateful Widget 传递 Null
问题描述
下面是 1 个用于页面的无状态小部件,1 个用于重复目的的有状态小部件。我意识到我尝试将最后一页的数据传递到此处,并返回初始值 = null 错误。
实际上这是我做模态路线的第三个地方
如图所示,_passData 来自最后一页,并将作为对象传递到下一页,我做得对吗?
发生 错误 错误图像
错误表明我传递的数据为空。但是当我在第 3 页以无状态测试传递的数据,在第 3 页无状态(即不调用有状态小部件来传递数据)时,有时它会工作,有时它不会
以下是第 3 页的代码
class EditDashboardCardDetailsScreen extends StatelessWidget {
static const routeName = '/EditDashboardDetail';
@override
Widget build(BuildContext context) {
final _deedID =
ModalRoute.of(context).settings.arguments as String; // is the id!
final _loadedDeed = Provider.of<DeedsData>(context).findByKey(_deedID);
String _tempTitle, //A
_tempDescription, //B
TextEditingController _textCtrlA = new TextEditingController();
TextEditingController _textCtrlB = new TextEditingController();
return Scaffold(
body: SafeArea(
child: CustomScrollView(
slivers: [
SliverAppBar(
pinned: true,
expandedHeight: 250.0,
actions: [],
flexibleSpace: FlexibleSpaceBar(
title: Text("Edit space"),
),
),
SliverList(
delegate: SliverChildListDelegate(
[
EditTextFieldForm(
initialValText: _loadedDeed.title,
labelText: "Title",
maxLines: 3,
textControllerTextForm: _textCtrlA,
),
],
),
),
],
),
),
);
}
}
class EditTextFieldForm extends StatefulWidget {
final String labelText;
final String initialValText;
final TextEditingController textControllerTextForm;
final int maxLines;
EditTextFieldForm({
@required this.labelText,
@required this.initialValText,
@required this.textControllerTextForm,
@required this.maxLines,
});
@override
_EditTextFieldFormState createState() => _EditTextFieldFormState();
}
class _EditTextFieldFormState extends State<EditTextFieldForm> {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(12),
child: TextFormField(
controller: widget.textControllerTextForm,
initialValue: widget.initialValText,
maxLines: widget.maxLines,
decoration: InputDecoration(
labelText: widget.labelText,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(5.0),
),
borderSide: BorderSide(color: Colors.blue),
),
),
),
);
}
}
请帮助我..非常感谢
我怀疑问题可能是
- 模态路由传递参数
- 第 3 页中的 stateless stateful 我做错了什么
编辑和更新:我尝试使用未来的构建器,但提出了另一个问题,我正在努力解决
新错误在此处输入图像描述
Future<String> _getID() async {
var _idpass = await ModalRoute.of(context).settings.arguments as String;
return _idpass;
}
......
FutureBuilder(
future: _getID(),
builder:
(context, snapshot) {
if (snapshot.hasData) {
return SliverList(
delegate: SliverChildListDelegate(
[
],
),
);
} else {
Center(
child: CircularProgressIndicator(),
);
}
},
)
解决方案
我怀疑findByKey
方法没有返回对象但_loadedDeed.title
已经被首先调用,所以它抛出了 null 错误。要解决此问题,我建议您使用FutureBuilder
.
FutureBuilder(
future: Provider.of<DeedsData>(context).findByKey(_deedID),
builder: (context, snapshot) {
if (snapshot.hasData) {
return EditTextFieldForm(
initialValText: snapshot.data.title,
labelText: "Title",
maxLines: 3,
textControllerTextForm: _textCtrlA,
);
} else {
return EditTextFieldForm(
initialValText: "",
labelText: "Title",
maxLines: 3,
textControllerTextForm: _textCtrlA,
);
}
});
编辑
你可以玩弄snapshot.connectionState
.
FutureBuilder(
future: _getID(),
builder:(context, snapshot) {
switch(snapshot.connectionState){
case ConnectionState.waiting:
return CircularProgressIndicator();
break;
case ConnectionState.active:
// your code;
case ConnectionState.done:
// your code;
default:
return Text("Error");
}
},
)
推荐阅读
- c# - 使用嵌套的 Async/Await 或在调用树的最后一个函数中使用它是否正确?
- javascript - 选项:插件不起作用
- apache - 将 PHP 错误记录到 Drupal 网站的自定义文件中
- php - 如何根据日期和 id 过滤掉 PHP 数组中的数据?
- c# - 如何通过 dapper 将 XML 字符串作为查询参数传递?
- spring-boot - 无法在主题中创建持久订阅者
- c++ - 使用带有 REQ/REP 模式的 zmq::proxy
- aws-cli - 如何将查询选项与 aws eks get-token 一起使用
- ios - 如何使用 Flutter APP 在 IOS 设备上播放 HTTP 直播?
- python-3.x - CSV 到嵌套 JSON