首页 > 解决方案 > 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),
          ),
        ),
      ),
    );
  }
}

请帮助我..非常感谢

我怀疑问题可能是

  1. 模态路由传递参数
  2. 第 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(),
                  );
                }
              },
            )

标签: flutterdartstateful

解决方案


我怀疑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");
               }
          },
        )

推荐阅读