Flutter 有状态小部件传递 Null

时间:2021-07-18 07:16:26

标签: flutter dart stateful

以下是页面的 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 页中的无状态状态我做错了什么

编辑和更新: 我尝试使用未来的构建器,但又提出了另一个问题,我正在努力解决

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

1 个答案:

答案 0 :(得分:0)

我怀疑 findByKey 方法还没有返回对象,但 _loadedDeed.title 已经被首先调用,所以它会抛出空错误。要解决此问题,我建议您使用 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");
               }
          },
        )