【问题标题】:Flutter Stateful Widget Passing NullFlutter Stateful Widget 传递 Null
【发布时间】:2021-09-26 07:32:06
【问题描述】:

以下是 1 个用于页面的无状态小部件,1 个用于重复目的的有状态小部件。 我意识到我尝试将最后一页的数据传递到此处,并返回初始值 = null 错误。

其实这是我做模态路线的第三个地方

Picture: 2nd page code, fetch data from first page, and modal to third

如图所示,_passData 来自最后一页,并作为对象传递到下一页,我做得对吗?

发生错误 Error Image

errors 显示我传递的数据为空。但是当我在第 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 页中的无状态有状态我做错了

编辑和更新: 我尝试使用 future builder 但提出了另一个问题,我正在努力解决

新错误enter image description here

      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(),
                  );
                }
              },
            )

【问题讨论】:

  • 你在哪里将数据传递到第三页?
  • @JohnJoe 从第一张图片中可以看到,最底部有一个 Navigator.of(context).pushReplacementNamed(EditDashboardCardDetailsS​​creen.routeName, arguments: _passData, );

标签: flutter dart stateful


【解决方案1】:

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

【讨论】:

  • 经过数小时的尝试和错误,我意识到问题很可能就像你提到的那样,_loadedDeed 还没有真正完成获取数据,但是 textformfield 已经完成运行,因此 initialVal 是空的,因此,未来的建设者我真的不能成功,有没有更多的方法可以在 textform 运行之前强制获取东西,否则返回一个圆形进度图标?
  • 是的,你可以使用swich-case查看snapshot.connectionState
  • 您好,前辈,可以帮我检查一下我对未来构建器的编辑和更新吗?如果我们能有一个非常快速的会议来解决这个问题,我将非常感激.. tq vmuch 提前。
  • 非常感谢前辈,想到还是遇到了一些问题,所以要明确一点,screen init->inter-page data accepted->render widget->substitute value,对吗?
猜你喜欢
  • 2020-06-06
  • 2020-02-27
  • 1970-01-01
  • 2019-07-26
  • 2018-12-06
  • 1970-01-01
  • 2018-01-28
  • 2019-07-08
  • 2021-05-26
相关资源
最近更新 更多