提问者:小点点

更新提供者模型时,类型“Null”不是“函数结果”的“String”类型的子类型


提供商版本和飞镖

provider: ^6.0.1
sdk: ">=2.12.0 <3.0.0"

我有两个屏幕。

屏幕A-我有一个表格。当您点击文本框时,它将打开屏幕B全屏模式。

屏幕B-您将从列表中选择一个项目,然后返回屏幕A。

在屏幕A文本框中,我用消费者方法包装。

Consumer<AgentModel>( //                    <--- Consumer
        builder: (context, agentModel, child) {
          agentController.text = agentModel.name.toString();
          return  AppInputWrapper(
            marginBottom: 'small',
            widgets: Column(
              children: [
                TextFormField(
                  style: appInputStyle,
                  keyboardType: TextInputType.text,
                  autocorrect: false,
                  enableSuggestions: false,
                  decoration: AppInputStyleDecoration.textFieldStyle(hintTextStr: "Agent"),
                  controller: agentController,
                  validator: (value) {
                    return validateAgent(value);
                  },
                  readOnly: true,
                  onTap: (){
                    Navigator.push(  context, MaterialPageRoute( builder: (context) => AgentFormComponent(),),);
                  },
                ),
              ],
            ),
          );
        }
    )

当第一个实例从屏幕B到屏幕A时,它没有从提供者加载值。我得到以下错误。

======== Exception caught by widgets library =======================================================
The following _TypeError was thrown building Consumer<AgentModel>(dirty, dependencies: [_InheritedProviderScope<AgentModel?>]):
type 'Null' is not a subtype of type 'String' of 'function result'

The relevant error-causing widget was: 
  Consumer<AgentModel> file:///Users/soundharraj/StudioProjects/LHMarine/lib/screens/jobs/create/form.dart:389:13
When the exception was thrown, this was the stack: 
#0      _Location.file (package:flutter/src/widgets/widget_inspector.dart)
#1      _Location.toJsonMap (package:flutter/src/widgets/widget_inspector.dart:2844:15)
#2      _Location.toJsonMap.<anonymous closure> (package:flutter/src/widgets/widget_inspector.dart:2853:42)
#3      MappedListIterable.elementAt (dart:_internal/iterable.dart:412:31)
#4      ListIterator.moveNext (dart:_internal/iterable.dart:341:26)
...
====================================================================================================
Reloaded 1 of 1128 libraries in 592ms.

我不确定如何检查提供者值为NULL。它轮到字符串。

谢啦


共1个答案

匿名用户

在ontab上编写您的函数时,使用Gesture检测器包装您的TextFormField。